1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for Medifield PNW Camera Imaging ISP subsystem.
4  *
5  * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
6  *
7  * Copyright (c) 2010 Silicon Hive www.siliconhive.com.
8  *
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.
12  *
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.
17  *
18  *
19  */
20 #include <linux/errno.h>
21 #include <linux/firmware.h>
22 #include <linux/pci.h>
23 #include <linux/interrupt.h>
24 #include <linux/io.h>
25 #include <linux/kernel.h>
26 #include <linux/kfifo.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/timer.h>
29 
30 #include <asm/iosf_mbi.h>
31 
32 #include <media/v4l2-event.h>
33 
34 #define CREATE_TRACE_POINTS
35 #include "atomisp_trace_event.h"
36 
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"
47 
48 #include <hmm/hmm.h>
49 
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"
57 #include "irq.h"
58 
59 #include "ia_css_types.h"
60 #include "ia_css_stream.h"
61 #include "ia_css_debug.h"
62 #include "bits.h"
63 
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 */
70 
71 union host {
72 	struct {
73 		void *kernel_ptr;
74 		void __user *user_ptr;
75 		int size;
76 	} scalar;
77 	struct {
78 		void *hmm_ptr;
79 	} ptr;
80 };
81 
82 static int atomisp_set_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id);
83 
84 /*
85  * get sensor:dis71430/ov2720 related info from v4l2_subdev->priv data field.
86  * subdev->priv is set in mrst.c
87  */
88 struct camera_mipi_info *atomisp_to_sensor_mipi_info(struct v4l2_subdev *sd)
89 {
90 	return (struct camera_mipi_info *)v4l2_get_subdev_hostdata(sd);
91 }
92 
93 /*
94  * get struct atomisp_video_pipe from v4l2 video_device
95  */
96 struct atomisp_video_pipe *atomisp_to_video_pipe(struct video_device *dev)
97 {
98 	return (struct atomisp_video_pipe *)
99 	       container_of(dev, struct atomisp_video_pipe, vdev);
100 }
101 
102 static unsigned short atomisp_get_sensor_fps(struct atomisp_sub_device *asd)
103 {
104 	struct v4l2_subdev_frame_interval fi = { 0 };
105 	struct atomisp_device *isp = asd->isp;
106 
107 	unsigned short fps = 0;
108 	int ret;
109 
110 	ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
111 			       video, g_frame_interval, &fi);
112 
113 	if (!ret && fi.interval.numerator)
114 		fps = fi.interval.denominator / fi.interval.numerator;
115 
116 	return fps;
117 }
118 
119 /*
120  * DFS progress is shown as follows:
121  * 1. Target frequency is calculated according to FPS/Resolution/ISP running
122  *    mode.
123  * 2. Ratio is calculated using formula: 2 * HPLL / target frequency - 1
124  *    with proper rounding.
125  * 3. Set ratio to ISPFREQ40, 1 to FREQVALID and ISPFREQGUAR40
126  *    to 200MHz in ISPSSPM1.
127  * 4. Wait for FREQVALID to be cleared by P-Unit.
128  * 5. Wait for field ISPFREQSTAT40 in ISPSSPM1 turn to ratio set in 3.
129  */
130 static int write_target_freq_to_hw(struct atomisp_device *isp,
131 				   unsigned int new_freq)
132 {
133 	unsigned int ratio, timeout, guar_ratio;
134 	u32 isp_sspm1 = 0;
135 	int i;
136 
137 	if (!isp->hpll_freq) {
138 		dev_err(isp->dev, "failed to get hpll_freq. no change to freq\n");
139 		return -EINVAL;
140 	}
141 
142 	iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
143 	if (isp_sspm1 & ISP_FREQ_VALID_MASK) {
144 		dev_dbg(isp->dev, "clearing ISPSSPM1 valid bit.\n");
145 		iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1,
146 			       isp_sspm1 & ~(1 << ISP_FREQ_VALID_OFFSET));
147 	}
148 
149 	ratio = (2 * isp->hpll_freq + new_freq / 2) / new_freq - 1;
150 	guar_ratio = (2 * isp->hpll_freq + 200 / 2) / 200 - 1;
151 
152 	iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
153 	isp_sspm1 &= ~(0x1F << ISP_REQ_FREQ_OFFSET);
154 
155 	for (i = 0; i < ISP_DFS_TRY_TIMES; i++) {
156 		iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1,
157 			       isp_sspm1
158 			       | ratio << ISP_REQ_FREQ_OFFSET
159 			       | 1 << ISP_FREQ_VALID_OFFSET
160 			       | guar_ratio << ISP_REQ_GUAR_FREQ_OFFSET);
161 
162 		iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
163 		timeout = 20;
164 		while ((isp_sspm1 & ISP_FREQ_VALID_MASK) && timeout) {
165 			iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
166 			dev_dbg(isp->dev, "waiting for ISPSSPM1 valid bit to be 0.\n");
167 			udelay(100);
168 			timeout--;
169 		}
170 
171 		if (timeout != 0)
172 			break;
173 	}
174 
175 	if (timeout == 0) {
176 		dev_err(isp->dev, "DFS failed due to HW error.\n");
177 		return -EINVAL;
178 	}
179 
180 	iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
181 	timeout = 10;
182 	while (((isp_sspm1 >> ISP_FREQ_STAT_OFFSET) != ratio) && timeout) {
183 		iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
184 		dev_dbg(isp->dev, "waiting for ISPSSPM1 status bit to be 0x%x.\n",
185 			new_freq);
186 		udelay(100);
187 		timeout--;
188 	}
189 	if (timeout == 0) {
190 		dev_err(isp->dev, "DFS target freq is rejected by HW.\n");
191 		return -EINVAL;
192 	}
193 
194 	return 0;
195 }
196 
197 int atomisp_freq_scaling(struct atomisp_device *isp,
198 			 enum atomisp_dfs_mode mode,
199 			 bool force)
200 {
201 	struct pci_dev *pdev = to_pci_dev(isp->dev);
202 	/* FIXME! Only use subdev[0] status yet */
203 	struct atomisp_sub_device *asd = &isp->asd[0];
204 	const struct atomisp_dfs_config *dfs;
205 	unsigned int new_freq;
206 	struct atomisp_freq_scaling_rule curr_rules;
207 	int i, ret;
208 	unsigned short fps = 0;
209 
210 	if ((pdev->device & ATOMISP_PCI_DEVICE_SOC_MASK) ==
211 	    ATOMISP_PCI_DEVICE_SOC_CHT && ATOMISP_USE_YUVPP(asd))
212 		isp->dfs = &dfs_config_cht_soc;
213 
214 	dfs = isp->dfs;
215 
216 	if (dfs->lowest_freq == 0 || dfs->max_freq_at_vmin == 0 ||
217 	    dfs->highest_freq == 0 || dfs->dfs_table_size == 0 ||
218 	    !dfs->dfs_table) {
219 		dev_err(isp->dev, "DFS configuration is invalid.\n");
220 		return -EINVAL;
221 	}
222 
223 	if (mode == ATOMISP_DFS_MODE_LOW) {
224 		new_freq = dfs->lowest_freq;
225 		goto done;
226 	}
227 
228 	if (mode == ATOMISP_DFS_MODE_MAX) {
229 		new_freq = dfs->highest_freq;
230 		goto done;
231 	}
232 
233 	fps = atomisp_get_sensor_fps(asd);
234 	if (fps == 0) {
235 		dev_info(isp->dev,
236 			 "Sensor didn't report FPS. Using DFS max mode.\n");
237 		new_freq = dfs->highest_freq;
238 		goto done;
239 	}
240 
241 	curr_rules.width = asd->fmt[asd->capture_pad].fmt.width;
242 	curr_rules.height = asd->fmt[asd->capture_pad].fmt.height;
243 	curr_rules.fps = fps;
244 	curr_rules.run_mode = asd->run_mode->val;
245 	/*
246 	 * For continuous mode, we need to make the capture setting applied
247 	 * since preview mode, because there is no chance to do this when
248 	 * starting image capture.
249 	 */
250 	if (asd->continuous_mode->val) {
251 		if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
252 			curr_rules.run_mode = ATOMISP_RUN_MODE_SDV;
253 		else
254 			curr_rules.run_mode =
255 			    ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE;
256 	}
257 
258 	/* search for the target frequency by looping freq rules*/
259 	for (i = 0; i < dfs->dfs_table_size; i++) {
260 		if (curr_rules.width != dfs->dfs_table[i].width &&
261 		    dfs->dfs_table[i].width != ISP_FREQ_RULE_ANY)
262 			continue;
263 		if (curr_rules.height != dfs->dfs_table[i].height &&
264 		    dfs->dfs_table[i].height != ISP_FREQ_RULE_ANY)
265 			continue;
266 		if (curr_rules.fps != dfs->dfs_table[i].fps &&
267 		    dfs->dfs_table[i].fps != ISP_FREQ_RULE_ANY)
268 			continue;
269 		if (curr_rules.run_mode != dfs->dfs_table[i].run_mode &&
270 		    dfs->dfs_table[i].run_mode != ISP_FREQ_RULE_ANY)
271 			continue;
272 		break;
273 	}
274 
275 	if (i == dfs->dfs_table_size)
276 		new_freq = dfs->max_freq_at_vmin;
277 	else
278 		new_freq = dfs->dfs_table[i].isp_freq;
279 
280 done:
281 	dev_dbg(isp->dev, "DFS target frequency=%d.\n", new_freq);
282 
283 	if ((new_freq == isp->sw_contex.running_freq) && !force)
284 		return 0;
285 
286 	dev_dbg(isp->dev, "Programming DFS frequency to %d\n", new_freq);
287 
288 	ret = write_target_freq_to_hw(isp, new_freq);
289 	if (!ret) {
290 		isp->sw_contex.running_freq = new_freq;
291 		trace_ipu_pstate(new_freq, -1);
292 	}
293 	return ret;
294 }
295 
296 /*
297  * reset and restore ISP
298  */
299 int atomisp_reset(struct atomisp_device *isp)
300 {
301 	/* Reset ISP by power-cycling it */
302 	int ret = 0;
303 
304 	dev_dbg(isp->dev, "%s\n", __func__);
305 
306 	ret = atomisp_power_off(isp->dev);
307 	if (ret < 0)
308 		dev_err(isp->dev, "atomisp_power_off failed, %d\n", ret);
309 
310 	ret = atomisp_power_on(isp->dev);
311 	if (ret < 0) {
312 		dev_err(isp->dev, "atomisp_power_on failed, %d\n", ret);
313 		isp->isp_fatal_error = true;
314 	}
315 
316 	return ret;
317 }
318 
319 /*
320  * interrupt disable functions
321  */
322 static void disable_isp_irq(enum hrt_isp_css_irq irq)
323 {
324 	irq_disable_channel(IRQ0_ID, irq);
325 
326 	if (irq != hrt_isp_css_irq_sp)
327 		return;
328 
329 	cnd_sp_irq_enable(SP0_ID, false);
330 }
331 
332 /*
333  * interrupt clean function
334  */
335 static void clear_isp_irq(enum hrt_isp_css_irq irq)
336 {
337 	irq_clear_all(IRQ0_ID);
338 }
339 
340 void atomisp_msi_irq_init(struct atomisp_device *isp)
341 {
342 	struct pci_dev *pdev = to_pci_dev(isp->dev);
343 	u32 msg32;
344 	u16 msg16;
345 
346 	pci_read_config_dword(pdev, PCI_MSI_CAPID, &msg32);
347 	msg32 |= 1 << MSI_ENABLE_BIT;
348 	pci_write_config_dword(pdev, PCI_MSI_CAPID, msg32);
349 
350 	msg32 = (1 << INTR_IER) | (1 << INTR_IIR);
351 	pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg32);
352 
353 	pci_read_config_word(pdev, PCI_COMMAND, &msg16);
354 	msg16 |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
355 		  PCI_COMMAND_INTX_DISABLE);
356 	pci_write_config_word(pdev, PCI_COMMAND, msg16);
357 }
358 
359 void atomisp_msi_irq_uninit(struct atomisp_device *isp)
360 {
361 	struct pci_dev *pdev = to_pci_dev(isp->dev);
362 	u32 msg32;
363 	u16 msg16;
364 
365 	pci_read_config_dword(pdev, PCI_MSI_CAPID, &msg32);
366 	msg32 &=  ~(1 << MSI_ENABLE_BIT);
367 	pci_write_config_dword(pdev, PCI_MSI_CAPID, msg32);
368 
369 	msg32 = 0x0;
370 	pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg32);
371 
372 	pci_read_config_word(pdev, PCI_COMMAND, &msg16);
373 	msg16 &= ~(PCI_COMMAND_MASTER);
374 	pci_write_config_word(pdev, PCI_COMMAND, msg16);
375 }
376 
377 static void atomisp_sof_event(struct atomisp_sub_device *asd)
378 {
379 	struct v4l2_event event = {0};
380 
381 	event.type = V4L2_EVENT_FRAME_SYNC;
382 	event.u.frame_sync.frame_sequence = atomic_read(&asd->sof_count);
383 
384 	v4l2_event_queue(asd->subdev.devnode, &event);
385 }
386 
387 void atomisp_eof_event(struct atomisp_sub_device *asd, uint8_t exp_id)
388 {
389 	struct v4l2_event event = {0};
390 
391 	event.type = V4L2_EVENT_FRAME_END;
392 	event.u.frame_sync.frame_sequence = exp_id;
393 
394 	v4l2_event_queue(asd->subdev.devnode, &event);
395 }
396 
397 static void atomisp_3a_stats_ready_event(struct atomisp_sub_device *asd,
398 	uint8_t exp_id)
399 {
400 	struct v4l2_event event = {0};
401 
402 	event.type = V4L2_EVENT_ATOMISP_3A_STATS_READY;
403 	event.u.frame_sync.frame_sequence = exp_id;
404 
405 	v4l2_event_queue(asd->subdev.devnode, &event);
406 }
407 
408 static void atomisp_metadata_ready_event(struct atomisp_sub_device *asd,
409 	enum atomisp_metadata_type md_type)
410 {
411 	struct v4l2_event event = {0};
412 
413 	event.type = V4L2_EVENT_ATOMISP_METADATA_READY;
414 	event.u.data[0] = md_type;
415 
416 	v4l2_event_queue(asd->subdev.devnode, &event);
417 }
418 
419 static void atomisp_reset_event(struct atomisp_sub_device *asd)
420 {
421 	struct v4l2_event event = {0};
422 
423 	event.type = V4L2_EVENT_ATOMISP_CSS_RESET;
424 
425 	v4l2_event_queue(asd->subdev.devnode, &event);
426 }
427 
428 static void print_csi_rx_errors(enum mipi_port_id port,
429 				struct atomisp_device *isp)
430 {
431 	u32 infos = 0;
432 
433 	atomisp_css_rx_get_irq_info(port, &infos);
434 
435 	dev_err(isp->dev, "CSI Receiver port %d errors:\n", port);
436 	if (infos & IA_CSS_RX_IRQ_INFO_BUFFER_OVERRUN)
437 		dev_err(isp->dev, "  buffer overrun");
438 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_SOT)
439 		dev_err(isp->dev, "  start-of-transmission error");
440 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_SOT_SYNC)
441 		dev_err(isp->dev, "  start-of-transmission sync error");
442 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_CONTROL)
443 		dev_err(isp->dev, "  control error");
444 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_ECC_DOUBLE)
445 		dev_err(isp->dev, "  2 or more ECC errors");
446 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_CRC)
447 		dev_err(isp->dev, "  CRC mismatch");
448 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_UNKNOWN_ID)
449 		dev_err(isp->dev, "  unknown error");
450 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_FRAME_SYNC)
451 		dev_err(isp->dev, "  frame sync error");
452 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_FRAME_DATA)
453 		dev_err(isp->dev, "  frame data error");
454 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_DATA_TIMEOUT)
455 		dev_err(isp->dev, "  data timeout");
456 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_UNKNOWN_ESC)
457 		dev_err(isp->dev, "  unknown escape command entry");
458 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_LINE_SYNC)
459 		dev_err(isp->dev, "  line sync error");
460 }
461 
462 /* Clear irq reg */
463 static void clear_irq_reg(struct atomisp_device *isp)
464 {
465 	struct pci_dev *pdev = to_pci_dev(isp->dev);
466 	u32 msg_ret;
467 
468 	pci_read_config_dword(pdev, PCI_INTERRUPT_CTRL, &msg_ret);
469 	msg_ret |= 1 << INTR_IIR;
470 	pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg_ret);
471 }
472 
473 static struct atomisp_sub_device *
474 __get_asd_from_port(struct atomisp_device *isp, enum mipi_port_id port)
475 {
476 	int i;
477 
478 	/* Check which isp subdev to send eof */
479 	for (i = 0; i < isp->num_of_streams; i++) {
480 		struct atomisp_sub_device *asd = &isp->asd[i];
481 		struct camera_mipi_info *mipi_info;
482 
483 		mipi_info = atomisp_to_sensor_mipi_info(
484 				isp->inputs[asd->input_curr].camera);
485 
486 		if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED &&
487 		    __get_mipi_port(isp, mipi_info->port) == port) {
488 			return asd;
489 		}
490 	}
491 
492 	return NULL;
493 }
494 
495 /* interrupt handling function*/
496 irqreturn_t atomisp_isr(int irq, void *dev)
497 {
498 	struct atomisp_device *isp = (struct atomisp_device *)dev;
499 	struct atomisp_sub_device *asd;
500 	struct atomisp_css_event eof_event;
501 	unsigned int irq_infos = 0;
502 	unsigned long flags;
503 	unsigned int i;
504 	int err;
505 
506 	spin_lock_irqsave(&isp->lock, flags);
507 
508 	if (!isp->css_initialized) {
509 		spin_unlock_irqrestore(&isp->lock, flags);
510 		return IRQ_HANDLED;
511 	}
512 	err = atomisp_css_irq_translate(isp, &irq_infos);
513 	if (err) {
514 		spin_unlock_irqrestore(&isp->lock, flags);
515 		return IRQ_NONE;
516 	}
517 
518 	clear_irq_reg(isp);
519 
520 	if (!atomisp_streaming_count(isp))
521 		goto out_nowake;
522 
523 	for (i = 0; i < isp->num_of_streams; i++) {
524 		asd = &isp->asd[i];
525 
526 		if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
527 			continue;
528 		/*
529 		 * Current SOF only support one stream, so the SOF only valid
530 		 * either solely one stream is running
531 		 */
532 		if (irq_infos & IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF) {
533 			atomic_inc(&asd->sof_count);
534 			atomisp_sof_event(asd);
535 
536 			/* If sequence_temp and sequence are the same
537 			 * there where no frames lost so we can increase
538 			 * sequence_temp.
539 			 * If not then processing of frame is still in progress
540 			 * and driver needs to keep old sequence_temp value.
541 			 * NOTE: There is assumption here that ISP will not
542 			 * start processing next frame from sensor before old
543 			 * one is completely done. */
544 			if (atomic_read(&asd->sequence) == atomic_read(
545 				&asd->sequence_temp))
546 				atomic_set(&asd->sequence_temp,
547 					   atomic_read(&asd->sof_count));
548 		}
549 		if (irq_infos & IA_CSS_IRQ_INFO_EVENTS_READY)
550 			atomic_set(&asd->sequence,
551 				   atomic_read(&asd->sequence_temp));
552 	}
553 
554 	if (irq_infos & IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF) {
555 		dev_dbg_ratelimited(isp->dev,
556 				    "irq:0x%x (SOF)\n",
557 				    irq_infos);
558 		irq_infos &= ~IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF;
559 	}
560 
561 	if ((irq_infos & IA_CSS_IRQ_INFO_INPUT_SYSTEM_ERROR) ||
562 	    (irq_infos & IA_CSS_IRQ_INFO_IF_ERROR)) {
563 		/* handle mipi receiver error */
564 		u32 rx_infos;
565 		enum mipi_port_id port;
566 
567 		for (port = MIPI_PORT0_ID; port <= MIPI_PORT2_ID;
568 		     port++) {
569 			print_csi_rx_errors(port, isp);
570 			atomisp_css_rx_get_irq_info(port, &rx_infos);
571 			atomisp_css_rx_clear_irq_info(port, rx_infos);
572 		}
573 	}
574 
575 	if (irq_infos & IA_CSS_IRQ_INFO_ISYS_EVENTS_READY) {
576 		while (ia_css_dequeue_isys_event(&eof_event.event) ==
577 		       0) {
578 			/* EOF Event does not have the css_pipe returned */
579 			asd = __get_asd_from_port(isp, eof_event.event.port);
580 			if (!asd) {
581 				dev_err(isp->dev, "%s: ISYS event, but no subdev.event:%d",
582 					__func__, eof_event.event.type);
583 				continue;
584 			}
585 
586 			atomisp_eof_event(asd, eof_event.event.exp_id);
587 			dev_dbg_ratelimited(isp->dev,
588 					    "%s ISYS event: EOF exp_id %d, asd %d\n",
589 					    __func__, eof_event.event.exp_id,
590 					    asd->index);
591 		}
592 
593 		irq_infos &= ~IA_CSS_IRQ_INFO_ISYS_EVENTS_READY;
594 		if (irq_infos == 0)
595 			goto out_nowake;
596 	}
597 
598 	spin_unlock_irqrestore(&isp->lock, flags);
599 
600 	dev_dbg_ratelimited(isp->dev, "irq:0x%x (unhandled)\n", irq_infos);
601 
602 	return IRQ_WAKE_THREAD;
603 
604 out_nowake:
605 	spin_unlock_irqrestore(&isp->lock, flags);
606 
607 	if (irq_infos)
608 		dev_dbg_ratelimited(isp->dev, "irq:0x%x (ignored, as not streaming anymore)\n",
609 				    irq_infos);
610 
611 	return IRQ_HANDLED;
612 }
613 
614 void atomisp_clear_css_buffer_counters(struct atomisp_sub_device *asd)
615 {
616 	int i;
617 
618 	memset(asd->s3a_bufs_in_css, 0, sizeof(asd->s3a_bufs_in_css));
619 	for (i = 0; i < ATOMISP_INPUT_STREAM_NUM; i++)
620 		memset(asd->metadata_bufs_in_css[i], 0,
621 		       sizeof(asd->metadata_bufs_in_css[i]));
622 	asd->dis_bufs_in_css = 0;
623 }
624 
625 /* 0x100000 is the start of dmem inside SP */
626 #define SP_DMEM_BASE	0x100000
627 
628 void dump_sp_dmem(struct atomisp_device *isp, unsigned int addr,
629 		  unsigned int size)
630 {
631 	unsigned int data = 0;
632 	unsigned int size32 = DIV_ROUND_UP(size, sizeof(u32));
633 
634 	dev_dbg(isp->dev, "atomisp mmio base: %p\n", isp->base);
635 	dev_dbg(isp->dev, "%s, addr:0x%x, size: %d, size32: %d\n", __func__,
636 		addr, size, size32);
637 	if (size32 * 4 + addr > 0x4000) {
638 		dev_err(isp->dev, "illegal size (%d) or addr (0x%x)\n",
639 			size32, addr);
640 		return;
641 	}
642 	addr += SP_DMEM_BASE;
643 	addr &= 0x003FFFFF;
644 	do {
645 		data = readl(isp->base + addr);
646 		dev_dbg(isp->dev, "%s, \t [0x%x]:0x%x\n", __func__, addr, data);
647 		addr += sizeof(u32);
648 	} while (--size32);
649 }
650 
651 int atomisp_buffers_in_css(struct atomisp_video_pipe *pipe)
652 {
653 	unsigned long irqflags;
654 	struct list_head *pos;
655 	int buffers_in_css = 0;
656 
657 	spin_lock_irqsave(&pipe->irq_lock, irqflags);
658 
659 	list_for_each(pos, &pipe->buffers_in_css)
660 		buffers_in_css++;
661 
662 	spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
663 
664 	return buffers_in_css;
665 }
666 
667 void atomisp_buffer_done(struct ia_css_frame *frame, enum vb2_buffer_state state)
668 {
669 	struct atomisp_video_pipe *pipe = vb_to_pipe(&frame->vb.vb2_buf);
670 
671 	lockdep_assert_held(&pipe->irq_lock);
672 
673 	frame->vb.vb2_buf.timestamp = ktime_get_ns();
674 	frame->vb.field = pipe->pix.field;
675 	frame->vb.sequence = atomic_read(&pipe->asd->sequence);
676 	list_del(&frame->queue);
677 	if (state == VB2_BUF_STATE_DONE)
678 		vb2_set_plane_payload(&frame->vb.vb2_buf, 0, pipe->pix.sizeimage);
679 	vb2_buffer_done(&frame->vb.vb2_buf, state);
680 }
681 
682 void atomisp_flush_video_pipe(struct atomisp_video_pipe *pipe, bool warn_on_css_frames)
683 {
684 	struct ia_css_frame *frame, *_frame;
685 	unsigned long irqflags;
686 
687 	spin_lock_irqsave(&pipe->irq_lock, irqflags);
688 
689 	list_for_each_entry_safe(frame, _frame, &pipe->buffers_in_css, queue) {
690 		if (warn_on_css_frames)
691 			dev_warn(pipe->isp->dev, "Warning: CSS frames queued on flush\n");
692 		atomisp_buffer_done(frame, VB2_BUF_STATE_ERROR);
693 	}
694 
695 	list_for_each_entry_safe(frame, _frame, &pipe->activeq, queue)
696 		atomisp_buffer_done(frame, VB2_BUF_STATE_ERROR);
697 
698 	list_for_each_entry_safe(frame, _frame, &pipe->buffers_waiting_for_param, queue) {
699 		pipe->frame_request_config_id[frame->vb.vb2_buf.index] = 0;
700 		atomisp_buffer_done(frame, VB2_BUF_STATE_ERROR);
701 	}
702 
703 	spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
704 }
705 
706 /* Returns queued buffers back to video-core */
707 void atomisp_flush_bufs_and_wakeup(struct atomisp_sub_device *asd)
708 {
709 	atomisp_flush_video_pipe(&asd->video_out_capture, false);
710 	atomisp_flush_video_pipe(&asd->video_out_vf, false);
711 	atomisp_flush_video_pipe(&asd->video_out_preview, false);
712 	atomisp_flush_video_pipe(&asd->video_out_video_capture, false);
713 }
714 
715 /* clean out the parameters that did not apply */
716 void atomisp_flush_params_queue(struct atomisp_video_pipe *pipe)
717 {
718 	struct atomisp_css_params_with_list *param;
719 
720 	while (!list_empty(&pipe->per_frame_params)) {
721 		param = list_entry(pipe->per_frame_params.next,
722 				   struct atomisp_css_params_with_list, list);
723 		list_del(&param->list);
724 		atomisp_free_css_parameters(&param->params);
725 		kvfree(param);
726 	}
727 }
728 
729 /* Re-queue per-frame parameters */
730 static void atomisp_recover_params_queue(struct atomisp_video_pipe *pipe)
731 {
732 	struct atomisp_css_params_with_list *param;
733 	int i;
734 
735 	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
736 		param = pipe->frame_params[i];
737 		if (param)
738 			list_add_tail(&param->list, &pipe->per_frame_params);
739 		pipe->frame_params[i] = NULL;
740 	}
741 	atomisp_handle_parameter_and_buffer(pipe);
742 }
743 
744 enum atomisp_metadata_type
745 atomisp_get_metadata_type(struct atomisp_sub_device *asd,
746 			  enum ia_css_pipe_id pipe_id)
747 {
748 	if (!asd->continuous_mode->val)
749 		return ATOMISP_MAIN_METADATA;
750 
751 	if (pipe_id == IA_CSS_PIPE_ID_CAPTURE) /* online capture pipe */
752 		return ATOMISP_SEC_METADATA;
753 	else
754 		return ATOMISP_MAIN_METADATA;
755 }
756 
757 void atomisp_buf_done(struct atomisp_sub_device *asd, int error,
758 		      enum ia_css_buffer_type buf_type,
759 		      enum ia_css_pipe_id css_pipe_id,
760 		      bool q_buffers, enum atomisp_input_stream_id stream_id)
761 {
762 	struct atomisp_video_pipe *pipe = NULL;
763 	struct atomisp_css_buffer buffer;
764 	bool requeue = false;
765 	unsigned long irqflags;
766 	struct ia_css_frame *frame = NULL;
767 	struct atomisp_s3a_buf *s3a_buf = NULL, *_s3a_buf_tmp, *s3a_iter;
768 	struct atomisp_dis_buf *dis_buf = NULL, *_dis_buf_tmp, *dis_iter;
769 	struct atomisp_metadata_buf *md_buf = NULL, *_md_buf_tmp, *md_iter;
770 	enum atomisp_metadata_type md_type;
771 	struct atomisp_device *isp = asd->isp;
772 	struct v4l2_control ctrl;
773 	int i, err;
774 
775 	lockdep_assert_held(&isp->mutex);
776 
777 	if (
778 	    buf_type != IA_CSS_BUFFER_TYPE_METADATA &&
779 	    buf_type != IA_CSS_BUFFER_TYPE_3A_STATISTICS &&
780 	    buf_type != IA_CSS_BUFFER_TYPE_DIS_STATISTICS &&
781 	    buf_type != IA_CSS_BUFFER_TYPE_OUTPUT_FRAME &&
782 	    buf_type != IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME &&
783 	    buf_type != IA_CSS_BUFFER_TYPE_RAW_OUTPUT_FRAME &&
784 	    buf_type != IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME &&
785 	    buf_type != IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME) {
786 		dev_err(isp->dev, "%s, unsupported buffer type: %d\n",
787 			__func__, buf_type);
788 		return;
789 	}
790 
791 	memset(&buffer, 0, sizeof(struct atomisp_css_buffer));
792 	buffer.css_buffer.type = buf_type;
793 	err = atomisp_css_dequeue_buffer(asd, stream_id, css_pipe_id,
794 					 buf_type, &buffer);
795 	if (err) {
796 		dev_err(isp->dev,
797 			"atomisp_css_dequeue_buffer failed: 0x%x\n", err);
798 		return;
799 	}
800 
801 	switch (buf_type) {
802 	case IA_CSS_BUFFER_TYPE_3A_STATISTICS:
803 		list_for_each_entry_safe(s3a_iter, _s3a_buf_tmp,
804 					 &asd->s3a_stats_in_css, list) {
805 			if (s3a_iter->s3a_data ==
806 			    buffer.css_buffer.data.stats_3a) {
807 				list_del_init(&s3a_iter->list);
808 				list_add_tail(&s3a_iter->list,
809 					      &asd->s3a_stats_ready);
810 				s3a_buf = s3a_iter;
811 				break;
812 			}
813 		}
814 
815 		asd->s3a_bufs_in_css[css_pipe_id]--;
816 		atomisp_3a_stats_ready_event(asd, buffer.css_buffer.exp_id);
817 		if (s3a_buf)
818 			dev_dbg(isp->dev, "%s: s3a stat with exp_id %d is ready\n",
819 				__func__, s3a_buf->s3a_data->exp_id);
820 		else
821 			dev_dbg(isp->dev, "%s: s3a stat is ready with no exp_id found\n",
822 				__func__);
823 		break;
824 	case IA_CSS_BUFFER_TYPE_METADATA:
825 		if (error)
826 			break;
827 
828 		md_type = atomisp_get_metadata_type(asd, css_pipe_id);
829 		list_for_each_entry_safe(md_iter, _md_buf_tmp,
830 					 &asd->metadata_in_css[md_type], list) {
831 			if (md_iter->metadata ==
832 			    buffer.css_buffer.data.metadata) {
833 				list_del_init(&md_iter->list);
834 				list_add_tail(&md_iter->list,
835 					      &asd->metadata_ready[md_type]);
836 				md_buf = md_iter;
837 				break;
838 			}
839 		}
840 		asd->metadata_bufs_in_css[stream_id][css_pipe_id]--;
841 		atomisp_metadata_ready_event(asd, md_type);
842 		if (md_buf)
843 			dev_dbg(isp->dev, "%s: metadata with exp_id %d is ready\n",
844 				__func__, md_buf->metadata->exp_id);
845 		else
846 			dev_dbg(isp->dev, "%s: metadata is ready with no exp_id found\n",
847 				__func__);
848 		break;
849 	case IA_CSS_BUFFER_TYPE_DIS_STATISTICS:
850 		list_for_each_entry_safe(dis_iter, _dis_buf_tmp,
851 					 &asd->dis_stats_in_css, list) {
852 			if (dis_iter->dis_data ==
853 			    buffer.css_buffer.data.stats_dvs) {
854 				spin_lock_irqsave(&asd->dis_stats_lock,
855 						  irqflags);
856 				list_del_init(&dis_iter->list);
857 				list_add(&dis_iter->list, &asd->dis_stats);
858 				asd->params.dis_proj_data_valid = true;
859 				spin_unlock_irqrestore(&asd->dis_stats_lock,
860 						       irqflags);
861 				dis_buf = dis_iter;
862 				break;
863 			}
864 		}
865 		asd->dis_bufs_in_css--;
866 		if (dis_buf)
867 			dev_dbg(isp->dev, "%s: dis stat with exp_id %d is ready\n",
868 				__func__, dis_buf->dis_data->exp_id);
869 		else
870 			dev_dbg(isp->dev, "%s: dis stat is ready with no exp_id found\n",
871 				__func__);
872 		break;
873 	case IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME:
874 	case IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME:
875 		frame = buffer.css_buffer.data.frame;
876 		if (!frame) {
877 			WARN_ON(1);
878 			break;
879 		}
880 		if (!frame->valid)
881 			error = true;
882 
883 		pipe = vb_to_pipe(&frame->vb.vb2_buf);
884 
885 		/* FIXME:
886 		 * YUVPP doesn't set postview exp_id correctlly in SDV mode.
887 		 * This is a WORKAROUND to set exp_id. see HSDES-1503911606.
888 		 */
889 		if (IS_BYT && buf_type == IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME &&
890 		    asd->continuous_mode->val && ATOMISP_USE_YUVPP(asd))
891 			frame->exp_id = (asd->postview_exp_id++) %
892 					(ATOMISP_MAX_EXP_ID + 1);
893 
894 		dev_dbg(isp->dev, "%s: vf frame with exp_id %d is ready\n",
895 			__func__, frame->exp_id);
896 		if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) {
897 			if (frame->flash_state
898 			    == IA_CSS_FRAME_FLASH_STATE_PARTIAL)
899 				dev_dbg(isp->dev, "%s thumb partially flashed\n",
900 					__func__);
901 			else if (frame->flash_state
902 				 == IA_CSS_FRAME_FLASH_STATE_FULL)
903 				dev_dbg(isp->dev, "%s thumb completely flashed\n",
904 					__func__);
905 			else
906 				dev_dbg(isp->dev, "%s thumb no flash in this frame\n",
907 					__func__);
908 		}
909 		pipe->frame_config_id[frame->vb.vb2_buf.index] = frame->isp_config_id;
910 		if (css_pipe_id == IA_CSS_PIPE_ID_CAPTURE &&
911 		    asd->pending_capture_request > 0) {
912 			err = atomisp_css_offline_capture_configure(asd,
913 				asd->params.offline_parm.num_captures,
914 				asd->params.offline_parm.skip_frames,
915 				asd->params.offline_parm.offset);
916 
917 			asd->pending_capture_request--;
918 
919 			dev_dbg(isp->dev, "Trigger capture again for new buffer. err=%d\n",
920 				err);
921 		}
922 		break;
923 	case IA_CSS_BUFFER_TYPE_OUTPUT_FRAME:
924 	case IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
925 		frame = buffer.css_buffer.data.frame;
926 		if (!frame) {
927 			WARN_ON(1);
928 			break;
929 		}
930 
931 		if (!frame->valid)
932 			error = true;
933 
934 		pipe = vb_to_pipe(&frame->vb.vb2_buf);
935 
936 		/* FIXME:
937 		 * YUVPP doesn't set preview exp_id correctlly in ZSL mode.
938 		 * This is a WORKAROUND to set exp_id. see HSDES-1503911606.
939 		 */
940 		if (IS_BYT && buf_type == IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME &&
941 		    asd->continuous_mode->val && ATOMISP_USE_YUVPP(asd))
942 			frame->exp_id = (asd->preview_exp_id++) %
943 					(ATOMISP_MAX_EXP_ID + 1);
944 
945 		dev_dbg(isp->dev, "%s: main frame with exp_id %d is ready\n",
946 			__func__, frame->exp_id);
947 
948 		i = frame->vb.vb2_buf.index;
949 
950 		/* free the parameters */
951 		if (pipe->frame_params[i]) {
952 			if (asd->params.dvs_6axis == pipe->frame_params[i]->params.dvs_6axis)
953 				asd->params.dvs_6axis = NULL;
954 			atomisp_free_css_parameters(&pipe->frame_params[i]->params);
955 			kvfree(pipe->frame_params[i]);
956 			pipe->frame_params[i] = NULL;
957 		}
958 
959 		pipe->frame_config_id[i] = frame->isp_config_id;
960 		ctrl.id = V4L2_CID_FLASH_MODE;
961 		if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) {
962 			if (frame->flash_state == IA_CSS_FRAME_FLASH_STATE_PARTIAL) {
963 				asd->frame_status[i] = ATOMISP_FRAME_STATUS_FLASH_PARTIAL;
964 				dev_dbg(isp->dev, "%s partially flashed\n", __func__);
965 			} else if (frame->flash_state == IA_CSS_FRAME_FLASH_STATE_FULL) {
966 				asd->frame_status[i] = ATOMISP_FRAME_STATUS_FLASH_EXPOSED;
967 				asd->params.num_flash_frames--;
968 				dev_dbg(isp->dev, "%s completely flashed\n", __func__);
969 			} else {
970 				asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK;
971 				dev_dbg(isp->dev, "%s no flash in this frame\n", __func__);
972 			}
973 
974 			/* Check if flashing sequence is done */
975 			if (asd->frame_status[i] == ATOMISP_FRAME_STATUS_FLASH_EXPOSED)
976 				asd->params.flash_state = ATOMISP_FLASH_DONE;
977 		} else if (isp->flash) {
978 			if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl) == 0 &&
979 			    ctrl.value == ATOMISP_FLASH_MODE_TORCH) {
980 				ctrl.id = V4L2_CID_FLASH_TORCH_INTENSITY;
981 				if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl) == 0 &&
982 				    ctrl.value > 0)
983 					asd->frame_status[i] = ATOMISP_FRAME_STATUS_FLASH_EXPOSED;
984 				else
985 					asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK;
986 			} else {
987 				asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK;
988 			}
989 		} else {
990 			asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK;
991 		}
992 
993 		asd->params.last_frame_status = asd->frame_status[i];
994 
995 		if (asd->continuous_mode->val) {
996 			if (css_pipe_id == IA_CSS_PIPE_ID_PREVIEW ||
997 			    css_pipe_id == IA_CSS_PIPE_ID_VIDEO) {
998 				asd->latest_preview_exp_id = frame->exp_id;
999 			} else if (css_pipe_id ==
1000 				   IA_CSS_PIPE_ID_CAPTURE) {
1001 				if (asd->run_mode->val ==
1002 				    ATOMISP_RUN_MODE_VIDEO)
1003 					dev_dbg(isp->dev, "SDV capture raw buffer id: %u\n",
1004 						frame->exp_id);
1005 				else
1006 					dev_dbg(isp->dev, "ZSL capture raw buffer id: %u\n",
1007 						frame->exp_id);
1008 			}
1009 		}
1010 		/*
1011 		 * Only after enabled the raw buffer lock
1012 		 * and in continuous mode.
1013 		 * in preview/video pipe, each buffer will
1014 		 * be locked automatically, so record it here.
1015 		 */
1016 		if (((css_pipe_id == IA_CSS_PIPE_ID_PREVIEW) ||
1017 		     (css_pipe_id == IA_CSS_PIPE_ID_VIDEO)) &&
1018 		    asd->enable_raw_buffer_lock->val &&
1019 		    asd->continuous_mode->val) {
1020 			atomisp_set_raw_buffer_bitmap(asd, frame->exp_id);
1021 			WARN_ON(frame->exp_id > ATOMISP_MAX_EXP_ID);
1022 		}
1023 
1024 		if (asd->params.css_update_params_needed) {
1025 			atomisp_apply_css_parameters(asd,
1026 						     &asd->params.css_param);
1027 			if (asd->params.css_param.update_flag.dz_config)
1028 				asd->params.config.dz_config = &asd->params.css_param.dz_config;
1029 			/* New global dvs 6axis config should be blocked
1030 			 * here if there's a buffer with per-frame parameters
1031 			 * pending in CSS frame buffer queue.
1032 			 * This is to aviod zooming vibration since global
1033 			 * parameters take effect immediately while
1034 			 * per-frame parameters are taken after previous
1035 			 * buffers in CSS got processed.
1036 			 */
1037 			if (asd->params.dvs_6axis)
1038 				atomisp_css_set_dvs_6axis(asd,
1039 							  asd->params.dvs_6axis);
1040 			else
1041 				asd->params.css_update_params_needed = false;
1042 			/* The update flag should not be cleaned here
1043 			 * since it is still going to be used to make up
1044 			 * following per-frame parameters.
1045 			 * This will introduce more copy work since each
1046 			 * time when updating global parameters, the whole
1047 			 * parameter set are applied.
1048 			 * FIXME: A new set of parameter copy functions can
1049 			 * be added to make up per-frame parameters based on
1050 			 * solid structures stored in asd->params.css_param
1051 			 * instead of using shadow pointers in update flag.
1052 			 */
1053 			atomisp_css_update_isp_params(asd);
1054 		}
1055 		break;
1056 	default:
1057 		break;
1058 	}
1059 	if (frame) {
1060 		spin_lock_irqsave(&pipe->irq_lock, irqflags);
1061 		atomisp_buffer_done(frame, error ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
1062 		spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
1063 	}
1064 
1065 	/*
1066 	 * Requeue should only be done for 3a and dis buffers.
1067 	 * Queue/dequeue order will change if driver recycles image buffers.
1068 	 */
1069 	if (requeue) {
1070 		err = atomisp_css_queue_buffer(asd,
1071 					       stream_id, css_pipe_id,
1072 					       buf_type, &buffer);
1073 		if (err)
1074 			dev_err(isp->dev, "%s, q to css fails: %d\n",
1075 				__func__, err);
1076 		return;
1077 	}
1078 	if (!error && q_buffers)
1079 		atomisp_qbuffers_to_css(asd);
1080 }
1081 
1082 void atomisp_delayed_init_work(struct work_struct *work)
1083 {
1084 	struct atomisp_sub_device *asd = container_of(work,
1085 					 struct atomisp_sub_device,
1086 					 delayed_init_work);
1087 	/*
1088 	 * to SOC camera, use yuvpp pipe and no support continuous mode.
1089 	 */
1090 	if (!ATOMISP_USE_YUVPP(asd)) {
1091 		struct v4l2_event event = {0};
1092 		struct ia_css_stream *stream;
1093 
1094 		stream = asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
1095 
1096 
1097 		if (ia_css_alloc_continuous_frame_remain(stream))
1098 			return;
1099 
1100 		ia_css_update_continuous_frames(stream);
1101 
1102 		event.type = V4L2_EVENT_ATOMISP_RAW_BUFFERS_ALLOC_DONE;
1103 		v4l2_event_queue(asd->subdev.devnode, &event);
1104 	}
1105 
1106 	/* signal streamon after delayed init is done */
1107 	asd->delayed_init = ATOMISP_DELAYED_INIT_DONE;
1108 	complete(&asd->init_done);
1109 }
1110 
1111 static void __atomisp_css_recover(struct atomisp_device *isp, bool isp_timeout)
1112 {
1113 	struct pci_dev *pdev = to_pci_dev(isp->dev);
1114 	enum ia_css_pipe_id css_pipe_id;
1115 	bool stream_restart[MAX_STREAM_NUM] = {0};
1116 	bool depth_mode = false;
1117 	int i, ret, depth_cnt = 0;
1118 	unsigned long flags;
1119 
1120 	lockdep_assert_held(&isp->mutex);
1121 
1122 	if (!atomisp_streaming_count(isp))
1123 		return;
1124 
1125 	atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF, false);
1126 
1127 	BUG_ON(isp->num_of_streams > MAX_STREAM_NUM);
1128 
1129 	for (i = 0; i < isp->num_of_streams; i++) {
1130 		struct atomisp_sub_device *asd = &isp->asd[i];
1131 
1132 		if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED &&
1133 		    !asd->stream_prepared)
1134 			continue;
1135 
1136 		depth_cnt++;
1137 
1138 		if (asd->delayed_init == ATOMISP_DELAYED_INIT_QUEUED)
1139 			cancel_work_sync(&asd->delayed_init_work);
1140 
1141 		complete(&asd->init_done);
1142 		asd->delayed_init = ATOMISP_DELAYED_INIT_NOT_QUEUED;
1143 
1144 		stream_restart[asd->index] = true;
1145 
1146 		spin_lock_irqsave(&isp->lock, flags);
1147 		asd->streaming = ATOMISP_DEVICE_STREAMING_STOPPING;
1148 		spin_unlock_irqrestore(&isp->lock, flags);
1149 
1150 		/* stream off sensor */
1151 		ret = v4l2_subdev_call(
1152 			  isp->inputs[asd->input_curr].
1153 			  camera, video, s_stream, 0);
1154 		if (ret)
1155 			dev_warn(isp->dev,
1156 				 "can't stop streaming on sensor!\n");
1157 
1158 		atomisp_clear_css_buffer_counters(asd);
1159 
1160 		css_pipe_id = atomisp_get_css_pipe_id(asd);
1161 		atomisp_css_stop(asd, css_pipe_id, true);
1162 
1163 		spin_lock_irqsave(&isp->lock, flags);
1164 		asd->streaming = ATOMISP_DEVICE_STREAMING_DISABLED;
1165 		spin_unlock_irqrestore(&isp->lock, flags);
1166 
1167 		asd->preview_exp_id = 1;
1168 		asd->postview_exp_id = 1;
1169 		/* notify HAL the CSS reset */
1170 		dev_dbg(isp->dev,
1171 			"send reset event to %s\n", asd->subdev.devnode->name);
1172 		atomisp_reset_event(asd);
1173 	}
1174 
1175 	/* clear irq */
1176 	disable_isp_irq(hrt_isp_css_irq_sp);
1177 	clear_isp_irq(hrt_isp_css_irq_sp);
1178 
1179 	/* Set the SRSE to 3 before resetting */
1180 	pci_write_config_dword(pdev, PCI_I_CONTROL,
1181 			       isp->saved_regs.i_control | MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK);
1182 
1183 	/* reset ISP and restore its state */
1184 	isp->isp_timeout = true;
1185 	atomisp_reset(isp);
1186 	isp->isp_timeout = false;
1187 
1188 	if (!isp_timeout) {
1189 		for (i = 0; i < isp->num_of_streams; i++) {
1190 			if (isp->asd[i].depth_mode->val)
1191 				return;
1192 		}
1193 	}
1194 
1195 	for (i = 0; i < isp->num_of_streams; i++) {
1196 		struct atomisp_sub_device *asd = &isp->asd[i];
1197 
1198 		if (!stream_restart[i])
1199 			continue;
1200 
1201 		if (isp->inputs[asd->input_curr].type != FILE_INPUT)
1202 			atomisp_css_input_set_mode(asd,
1203 						   IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
1204 
1205 		css_pipe_id = atomisp_get_css_pipe_id(asd);
1206 		if (atomisp_css_start(asd, css_pipe_id, true)) {
1207 			dev_warn(isp->dev,
1208 				 "start SP failed, so do not set streaming to be enable!\n");
1209 		} else {
1210 			spin_lock_irqsave(&isp->lock, flags);
1211 			asd->streaming = ATOMISP_DEVICE_STREAMING_ENABLED;
1212 			spin_unlock_irqrestore(&isp->lock, flags);
1213 		}
1214 
1215 		atomisp_csi2_configure(asd);
1216 	}
1217 
1218 	atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF,
1219 			       atomisp_css_valid_sof(isp));
1220 
1221 	if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, true) < 0)
1222 		dev_dbg(isp->dev, "DFS auto failed while recovering!\n");
1223 
1224 	for (i = 0; i < isp->num_of_streams; i++) {
1225 		struct atomisp_sub_device *asd;
1226 
1227 		asd = &isp->asd[i];
1228 
1229 		if (!stream_restart[i])
1230 			continue;
1231 
1232 		if (asd->continuous_mode->val &&
1233 		    asd->delayed_init == ATOMISP_DELAYED_INIT_NOT_QUEUED) {
1234 			reinit_completion(&asd->init_done);
1235 			asd->delayed_init = ATOMISP_DELAYED_INIT_QUEUED;
1236 			queue_work(asd->delayed_init_workq,
1237 				   &asd->delayed_init_work);
1238 		}
1239 		/*
1240 		 * dequeueing buffers is not needed. CSS will recycle
1241 		 * buffers that it has.
1242 		 */
1243 		atomisp_flush_bufs_and_wakeup(asd);
1244 
1245 		/* Requeue unprocessed per-frame parameters. */
1246 		atomisp_recover_params_queue(&asd->video_out_capture);
1247 		atomisp_recover_params_queue(&asd->video_out_preview);
1248 		atomisp_recover_params_queue(&asd->video_out_video_capture);
1249 
1250 		if ((asd->depth_mode->val) &&
1251 		    (depth_cnt == ATOMISP_DEPTH_SENSOR_STREAMON_COUNT)) {
1252 			depth_mode = true;
1253 			continue;
1254 		}
1255 
1256 		ret = v4l2_subdev_call(
1257 			  isp->inputs[asd->input_curr].camera, video,
1258 			  s_stream, 1);
1259 		if (ret)
1260 			dev_warn(isp->dev,
1261 				 "can't start streaming on sensor!\n");
1262 	}
1263 
1264 	if (depth_mode) {
1265 		if (atomisp_stream_on_master_slave_sensor(isp, true))
1266 			dev_warn(isp->dev,
1267 				 "master slave sensor stream on failed!\n");
1268 	}
1269 }
1270 
1271 void atomisp_assert_recovery_work(struct work_struct *work)
1272 {
1273 	struct atomisp_device *isp = container_of(work, struct atomisp_device,
1274 						  assert_recovery_work);
1275 
1276 	mutex_lock(&isp->mutex);
1277 	__atomisp_css_recover(isp, true);
1278 	mutex_unlock(&isp->mutex);
1279 }
1280 
1281 void atomisp_css_flush(struct atomisp_device *isp)
1282 {
1283 	/* Start recover */
1284 	__atomisp_css_recover(isp, false);
1285 
1286 	dev_dbg(isp->dev, "atomisp css flush done\n");
1287 }
1288 
1289 void atomisp_setup_flash(struct atomisp_sub_device *asd)
1290 {
1291 	struct atomisp_device *isp = asd->isp;
1292 	struct v4l2_control ctrl;
1293 
1294 	if (!isp->flash)
1295 		return;
1296 
1297 	if (asd->params.flash_state != ATOMISP_FLASH_REQUESTED &&
1298 	    asd->params.flash_state != ATOMISP_FLASH_DONE)
1299 		return;
1300 
1301 	if (asd->params.num_flash_frames) {
1302 		/* make sure the timeout is set before setting flash mode */
1303 		ctrl.id = V4L2_CID_FLASH_TIMEOUT;
1304 		ctrl.value = FLASH_TIMEOUT;
1305 
1306 		if (v4l2_s_ctrl(NULL, isp->flash->ctrl_handler, &ctrl)) {
1307 			dev_err(isp->dev, "flash timeout configure failed\n");
1308 			return;
1309 		}
1310 
1311 		ia_css_stream_request_flash(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream);
1312 
1313 		asd->params.flash_state = ATOMISP_FLASH_ONGOING;
1314 	} else {
1315 		asd->params.flash_state = ATOMISP_FLASH_IDLE;
1316 	}
1317 }
1318 
1319 irqreturn_t atomisp_isr_thread(int irq, void *isp_ptr)
1320 {
1321 	struct atomisp_device *isp = isp_ptr;
1322 	unsigned long flags;
1323 	bool frame_done_found[MAX_STREAM_NUM] = {0};
1324 	bool css_pipe_done[MAX_STREAM_NUM] = {0};
1325 	unsigned int i;
1326 	struct atomisp_sub_device *asd;
1327 
1328 	dev_dbg(isp->dev, ">%s\n", __func__);
1329 
1330 	spin_lock_irqsave(&isp->lock, flags);
1331 
1332 	if (!atomisp_streaming_count(isp)) {
1333 		spin_unlock_irqrestore(&isp->lock, flags);
1334 		return IRQ_HANDLED;
1335 	}
1336 
1337 	spin_unlock_irqrestore(&isp->lock, flags);
1338 
1339 	/*
1340 	 * The standard CSS2.0 API tells the following calling sequence of
1341 	 * dequeue ready buffers:
1342 	 * while (ia_css_dequeue_psys_event(...)) {
1343 	 *	switch (event.type) {
1344 	 *	...
1345 	 *	ia_css_pipe_dequeue_buffer()
1346 	 *	}
1347 	 * }
1348 	 * That is, dequeue event and buffer are one after another.
1349 	 *
1350 	 * But the following implementation is to first deuque all the event
1351 	 * to a FIFO, then process the event in the FIFO.
1352 	 * This will not have issue in single stream mode, but it do have some
1353 	 * issue in multiple stream case. The issue is that
1354 	 * ia_css_pipe_dequeue_buffer() will not return the corrent buffer in
1355 	 * a specific pipe.
1356 	 *
1357 	 * This is due to ia_css_pipe_dequeue_buffer() does not take the
1358 	 * ia_css_pipe parameter.
1359 	 *
1360 	 * So:
1361 	 * For CSS2.0: we change the way to not dequeue all the event at one
1362 	 * time, instead, dequue one and process one, then another
1363 	 */
1364 	mutex_lock(&isp->mutex);
1365 	if (atomisp_css_isr_thread(isp, frame_done_found, css_pipe_done))
1366 		goto out;
1367 
1368 	for (i = 0; i < isp->num_of_streams; i++) {
1369 		asd = &isp->asd[i];
1370 		if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1371 			continue;
1372 		atomisp_setup_flash(asd);
1373 	}
1374 out:
1375 	mutex_unlock(&isp->mutex);
1376 	dev_dbg(isp->dev, "<%s\n", __func__);
1377 
1378 	return IRQ_HANDLED;
1379 }
1380 
1381 /*
1382  * Get internal fmt according to V4L2 fmt
1383  */
1384 static enum ia_css_frame_format
1385 v4l2_fmt_to_sh_fmt(u32 fmt)
1386 {
1387 	switch (fmt) {
1388 	case V4L2_PIX_FMT_YUV420:
1389 				return IA_CSS_FRAME_FORMAT_YUV420;
1390 	case V4L2_PIX_FMT_YVU420:
1391 		return IA_CSS_FRAME_FORMAT_YV12;
1392 	case V4L2_PIX_FMT_YUV422P:
1393 		return IA_CSS_FRAME_FORMAT_YUV422;
1394 	case V4L2_PIX_FMT_YUV444:
1395 		return IA_CSS_FRAME_FORMAT_YUV444;
1396 	case V4L2_PIX_FMT_NV12:
1397 		return IA_CSS_FRAME_FORMAT_NV12;
1398 	case V4L2_PIX_FMT_NV21:
1399 		return IA_CSS_FRAME_FORMAT_NV21;
1400 	case V4L2_PIX_FMT_NV16:
1401 		return IA_CSS_FRAME_FORMAT_NV16;
1402 	case V4L2_PIX_FMT_NV61:
1403 		return IA_CSS_FRAME_FORMAT_NV61;
1404 	case V4L2_PIX_FMT_UYVY:
1405 		return IA_CSS_FRAME_FORMAT_UYVY;
1406 	case V4L2_PIX_FMT_YUYV:
1407 		return IA_CSS_FRAME_FORMAT_YUYV;
1408 	case V4L2_PIX_FMT_RGB24:
1409 		return IA_CSS_FRAME_FORMAT_PLANAR_RGB888;
1410 	case V4L2_PIX_FMT_RGB32:
1411 		return IA_CSS_FRAME_FORMAT_RGBA888;
1412 	case V4L2_PIX_FMT_RGB565:
1413 		return IA_CSS_FRAME_FORMAT_RGB565;
1414 #if 0
1415 	case V4L2_PIX_FMT_JPEG:
1416 	case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
1417 		return IA_CSS_FRAME_FORMAT_BINARY_8;
1418 #endif
1419 	case V4L2_PIX_FMT_SBGGR16:
1420 	case V4L2_PIX_FMT_SBGGR10:
1421 	case V4L2_PIX_FMT_SGBRG10:
1422 	case V4L2_PIX_FMT_SGRBG10:
1423 	case V4L2_PIX_FMT_SRGGB10:
1424 	case V4L2_PIX_FMT_SBGGR12:
1425 	case V4L2_PIX_FMT_SGBRG12:
1426 	case V4L2_PIX_FMT_SGRBG12:
1427 	case V4L2_PIX_FMT_SRGGB12:
1428 	case V4L2_PIX_FMT_SBGGR8:
1429 	case V4L2_PIX_FMT_SGBRG8:
1430 	case V4L2_PIX_FMT_SGRBG8:
1431 	case V4L2_PIX_FMT_SRGGB8:
1432 		return IA_CSS_FRAME_FORMAT_RAW;
1433 	default:
1434 		return -EINVAL;
1435 	}
1436 }
1437 
1438 /*
1439  * raw format match between SH format and V4L2 format
1440  */
1441 static int raw_output_format_match_input(u32 input, u32 output)
1442 {
1443 	if ((input == ATOMISP_INPUT_FORMAT_RAW_12) &&
1444 	    ((output == V4L2_PIX_FMT_SRGGB12) ||
1445 	     (output == V4L2_PIX_FMT_SGRBG12) ||
1446 	     (output == V4L2_PIX_FMT_SBGGR12) ||
1447 	     (output == V4L2_PIX_FMT_SGBRG12)))
1448 		return 0;
1449 
1450 	if ((input == ATOMISP_INPUT_FORMAT_RAW_10) &&
1451 	    ((output == V4L2_PIX_FMT_SRGGB10) ||
1452 	     (output == V4L2_PIX_FMT_SGRBG10) ||
1453 	     (output == V4L2_PIX_FMT_SBGGR10) ||
1454 	     (output == V4L2_PIX_FMT_SGBRG10)))
1455 		return 0;
1456 
1457 	if ((input == ATOMISP_INPUT_FORMAT_RAW_8) &&
1458 	    ((output == V4L2_PIX_FMT_SRGGB8) ||
1459 	     (output == V4L2_PIX_FMT_SGRBG8) ||
1460 	     (output == V4L2_PIX_FMT_SBGGR8) ||
1461 	     (output == V4L2_PIX_FMT_SGBRG8)))
1462 		return 0;
1463 
1464 	if ((input == ATOMISP_INPUT_FORMAT_RAW_16) && (output == V4L2_PIX_FMT_SBGGR16))
1465 		return 0;
1466 
1467 	return -EINVAL;
1468 }
1469 
1470 u32 atomisp_get_pixel_depth(u32 pixelformat)
1471 {
1472 	switch (pixelformat) {
1473 	case V4L2_PIX_FMT_YUV420:
1474 	case V4L2_PIX_FMT_NV12:
1475 	case V4L2_PIX_FMT_NV21:
1476 	case V4L2_PIX_FMT_YVU420:
1477 		return 12;
1478 	case V4L2_PIX_FMT_YUV422P:
1479 	case V4L2_PIX_FMT_YUYV:
1480 	case V4L2_PIX_FMT_UYVY:
1481 	case V4L2_PIX_FMT_NV16:
1482 	case V4L2_PIX_FMT_NV61:
1483 	case V4L2_PIX_FMT_RGB565:
1484 	case V4L2_PIX_FMT_SBGGR16:
1485 	case V4L2_PIX_FMT_SBGGR12:
1486 	case V4L2_PIX_FMT_SGBRG12:
1487 	case V4L2_PIX_FMT_SGRBG12:
1488 	case V4L2_PIX_FMT_SRGGB12:
1489 	case V4L2_PIX_FMT_SBGGR10:
1490 	case V4L2_PIX_FMT_SGBRG10:
1491 	case V4L2_PIX_FMT_SGRBG10:
1492 	case V4L2_PIX_FMT_SRGGB10:
1493 		return 16;
1494 	case V4L2_PIX_FMT_RGB24:
1495 	case V4L2_PIX_FMT_YUV444:
1496 		return 24;
1497 	case V4L2_PIX_FMT_RGB32:
1498 		return 32;
1499 	case V4L2_PIX_FMT_JPEG:
1500 	case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
1501 	case V4L2_PIX_FMT_SBGGR8:
1502 	case V4L2_PIX_FMT_SGBRG8:
1503 	case V4L2_PIX_FMT_SGRBG8:
1504 	case V4L2_PIX_FMT_SRGGB8:
1505 		return 8;
1506 	default:
1507 		return 8 * 2;	/* raw type now */
1508 	}
1509 }
1510 
1511 bool atomisp_is_mbuscode_raw(uint32_t code)
1512 {
1513 	return code >= 0x3000 && code < 0x4000;
1514 }
1515 
1516 /*
1517  * ISP features control function
1518  */
1519 
1520 /*
1521  * Set ISP capture mode based on current settings
1522  */
1523 static void atomisp_update_capture_mode(struct atomisp_sub_device *asd)
1524 {
1525 	if (asd->params.gdc_cac_en)
1526 		atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_ADVANCED);
1527 	else if (asd->params.low_light)
1528 		atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_LOW_LIGHT);
1529 	else if (asd->video_out_capture.sh_fmt == IA_CSS_FRAME_FORMAT_RAW)
1530 		atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
1531 	else
1532 		atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_PRIMARY);
1533 }
1534 
1535 /* ISP2401 */
1536 int atomisp_set_sensor_runmode(struct atomisp_sub_device *asd,
1537 			       struct atomisp_s_runmode *runmode)
1538 {
1539 	struct atomisp_device *isp = asd->isp;
1540 	struct v4l2_ctrl *c;
1541 	int ret = 0;
1542 
1543 	if (!(runmode && (runmode->mode & RUNMODE_MASK)))
1544 		return -EINVAL;
1545 
1546 	mutex_lock(asd->ctrl_handler.lock);
1547 	c = v4l2_ctrl_find(isp->inputs[asd->input_curr].camera->ctrl_handler,
1548 			   V4L2_CID_RUN_MODE);
1549 
1550 	if (c)
1551 		ret = v4l2_ctrl_s_ctrl(c, runmode->mode);
1552 
1553 	mutex_unlock(asd->ctrl_handler.lock);
1554 	return ret;
1555 }
1556 
1557 /*
1558  * Function to enable/disable lens geometry distortion correction (GDC) and
1559  * chromatic aberration correction (CAC)
1560  */
1561 int atomisp_gdc_cac(struct atomisp_sub_device *asd, int flag,
1562 		    __s32 *value)
1563 {
1564 	if (flag == 0) {
1565 		*value = asd->params.gdc_cac_en;
1566 		return 0;
1567 	}
1568 
1569 	asd->params.gdc_cac_en = !!*value;
1570 	if (asd->params.gdc_cac_en) {
1571 		asd->params.config.morph_table = asd->params.css_param.morph_table;
1572 	} else {
1573 		asd->params.config.morph_table = NULL;
1574 	}
1575 	asd->params.css_update_params_needed = true;
1576 	atomisp_update_capture_mode(asd);
1577 	return 0;
1578 }
1579 
1580 /*
1581  * Function to enable/disable low light mode including ANR
1582  */
1583 int atomisp_low_light(struct atomisp_sub_device *asd, int flag,
1584 		      __s32 *value)
1585 {
1586 	if (flag == 0) {
1587 		*value = asd->params.low_light;
1588 		return 0;
1589 	}
1590 
1591 	asd->params.low_light = (*value != 0);
1592 	atomisp_update_capture_mode(asd);
1593 	return 0;
1594 }
1595 
1596 /*
1597  * Function to enable/disable extra noise reduction (XNR) in low light
1598  * condition
1599  */
1600 int atomisp_xnr(struct atomisp_sub_device *asd, int flag,
1601 		int *xnr_enable)
1602 {
1603 	if (flag == 0) {
1604 		*xnr_enable = asd->params.xnr_en;
1605 		return 0;
1606 	}
1607 
1608 	atomisp_css_capture_enable_xnr(asd, !!*xnr_enable);
1609 
1610 	return 0;
1611 }
1612 
1613 /*
1614  * Function to configure bayer noise reduction
1615  */
1616 int atomisp_nr(struct atomisp_sub_device *asd, int flag,
1617 	       struct atomisp_nr_config *arg)
1618 {
1619 	if (flag == 0) {
1620 		/* Get nr config from current setup */
1621 		if (atomisp_css_get_nr_config(asd, arg))
1622 			return -EINVAL;
1623 	} else {
1624 		/* Set nr config to isp parameters */
1625 		memcpy(&asd->params.css_param.nr_config, arg,
1626 		       sizeof(struct ia_css_nr_config));
1627 		asd->params.config.nr_config = &asd->params.css_param.nr_config;
1628 		asd->params.css_update_params_needed = true;
1629 	}
1630 	return 0;
1631 }
1632 
1633 /*
1634  * Function to configure temporal noise reduction (TNR)
1635  */
1636 int atomisp_tnr(struct atomisp_sub_device *asd, int flag,
1637 		struct atomisp_tnr_config *config)
1638 {
1639 	/* Get tnr config from current setup */
1640 	if (flag == 0) {
1641 		/* Get tnr config from current setup */
1642 		if (atomisp_css_get_tnr_config(asd, config))
1643 			return -EINVAL;
1644 	} else {
1645 		/* Set tnr config to isp parameters */
1646 		memcpy(&asd->params.css_param.tnr_config, config,
1647 		       sizeof(struct ia_css_tnr_config));
1648 		asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
1649 		asd->params.css_update_params_needed = true;
1650 	}
1651 
1652 	return 0;
1653 }
1654 
1655 /*
1656  * Function to configure black level compensation
1657  */
1658 int atomisp_black_level(struct atomisp_sub_device *asd, int flag,
1659 			struct atomisp_ob_config *config)
1660 {
1661 	if (flag == 0) {
1662 		/* Get ob config from current setup */
1663 		if (atomisp_css_get_ob_config(asd, config))
1664 			return -EINVAL;
1665 	} else {
1666 		/* Set ob config to isp parameters */
1667 		memcpy(&asd->params.css_param.ob_config, config,
1668 		       sizeof(struct ia_css_ob_config));
1669 		asd->params.config.ob_config = &asd->params.css_param.ob_config;
1670 		asd->params.css_update_params_needed = true;
1671 	}
1672 
1673 	return 0;
1674 }
1675 
1676 /*
1677  * Function to configure edge enhancement
1678  */
1679 int atomisp_ee(struct atomisp_sub_device *asd, int flag,
1680 	       struct atomisp_ee_config *config)
1681 {
1682 	if (flag == 0) {
1683 		/* Get ee config from current setup */
1684 		if (atomisp_css_get_ee_config(asd, config))
1685 			return -EINVAL;
1686 	} else {
1687 		/* Set ee config to isp parameters */
1688 		memcpy(&asd->params.css_param.ee_config, config,
1689 		       sizeof(asd->params.css_param.ee_config));
1690 		asd->params.config.ee_config = &asd->params.css_param.ee_config;
1691 		asd->params.css_update_params_needed = true;
1692 	}
1693 
1694 	return 0;
1695 }
1696 
1697 /*
1698  * Function to update Gamma table for gamma, brightness and contrast config
1699  */
1700 int atomisp_gamma(struct atomisp_sub_device *asd, int flag,
1701 		  struct atomisp_gamma_table *config)
1702 {
1703 	if (flag == 0) {
1704 		/* Get gamma table from current setup */
1705 		if (atomisp_css_get_gamma_table(asd, config))
1706 			return -EINVAL;
1707 	} else {
1708 		/* Set gamma table to isp parameters */
1709 		memcpy(&asd->params.css_param.gamma_table, config,
1710 		       sizeof(asd->params.css_param.gamma_table));
1711 		asd->params.config.gamma_table = &asd->params.css_param.gamma_table;
1712 	}
1713 
1714 	return 0;
1715 }
1716 
1717 /*
1718  * Function to update Ctc table for Chroma Enhancement
1719  */
1720 int atomisp_ctc(struct atomisp_sub_device *asd, int flag,
1721 		struct atomisp_ctc_table *config)
1722 {
1723 	if (flag == 0) {
1724 		/* Get ctc table from current setup */
1725 		if (atomisp_css_get_ctc_table(asd, config))
1726 			return -EINVAL;
1727 	} else {
1728 		/* Set ctc table to isp parameters */
1729 		memcpy(&asd->params.css_param.ctc_table, config,
1730 		       sizeof(asd->params.css_param.ctc_table));
1731 		atomisp_css_set_ctc_table(asd, &asd->params.css_param.ctc_table);
1732 	}
1733 
1734 	return 0;
1735 }
1736 
1737 /*
1738  * Function to update gamma correction parameters
1739  */
1740 int atomisp_gamma_correction(struct atomisp_sub_device *asd, int flag,
1741 			     struct atomisp_gc_config *config)
1742 {
1743 	if (flag == 0) {
1744 		/* Get gamma correction params from current setup */
1745 		if (atomisp_css_get_gc_config(asd, config))
1746 			return -EINVAL;
1747 	} else {
1748 		/* Set gamma correction params to isp parameters */
1749 		memcpy(&asd->params.css_param.gc_config, config,
1750 		       sizeof(asd->params.css_param.gc_config));
1751 		asd->params.config.gc_config = &asd->params.css_param.gc_config;
1752 		asd->params.css_update_params_needed = true;
1753 	}
1754 
1755 	return 0;
1756 }
1757 
1758 /*
1759  * Function to update narrow gamma flag
1760  */
1761 int atomisp_formats(struct atomisp_sub_device *asd, int flag,
1762 		    struct atomisp_formats_config *config)
1763 {
1764 	if (flag == 0) {
1765 		/* Get narrow gamma flag from current setup */
1766 		if (atomisp_css_get_formats_config(asd, config))
1767 			return -EINVAL;
1768 	} else {
1769 		/* Set narrow gamma flag to isp parameters */
1770 		memcpy(&asd->params.css_param.formats_config, config,
1771 		       sizeof(asd->params.css_param.formats_config));
1772 		asd->params.config.formats_config = &asd->params.css_param.formats_config;
1773 	}
1774 
1775 	return 0;
1776 }
1777 
1778 void atomisp_free_internal_buffers(struct atomisp_sub_device *asd)
1779 {
1780 	atomisp_free_css_parameters(&asd->params.css_param);
1781 
1782 	if (asd->raw_output_frame) {
1783 		ia_css_frame_free(asd->raw_output_frame);
1784 		asd->raw_output_frame = NULL;
1785 	}
1786 }
1787 
1788 static void atomisp_update_grid_info(struct atomisp_sub_device *asd,
1789 				     enum ia_css_pipe_id pipe_id,
1790 				     int source_pad)
1791 {
1792 	struct atomisp_device *isp = asd->isp;
1793 	int err;
1794 
1795 	if (atomisp_css_get_grid_info(asd, pipe_id, source_pad))
1796 		return;
1797 
1798 	/* We must free all buffers because they no longer match
1799 	   the grid size. */
1800 	atomisp_css_free_stat_buffers(asd);
1801 
1802 	err = atomisp_alloc_css_stat_bufs(asd, ATOMISP_INPUT_STREAM_GENERAL);
1803 	if (err) {
1804 		dev_err(isp->dev, "stat_buf allocate error\n");
1805 		goto err;
1806 	}
1807 
1808 	if (atomisp_alloc_3a_output_buf(asd)) {
1809 		/* Failure for 3A buffers does not influence DIS buffers */
1810 		if (asd->params.s3a_output_bytes != 0) {
1811 			/* For SOC sensor happens s3a_output_bytes == 0,
1812 			 * using if condition to exclude false error log */
1813 			dev_err(isp->dev, "Failed to allocate memory for 3A statistics\n");
1814 		}
1815 		goto err;
1816 	}
1817 
1818 	if (atomisp_alloc_dis_coef_buf(asd)) {
1819 		dev_err(isp->dev,
1820 			"Failed to allocate memory for DIS statistics\n");
1821 		goto err;
1822 	}
1823 
1824 	if (atomisp_alloc_metadata_output_buf(asd)) {
1825 		dev_err(isp->dev, "Failed to allocate memory for metadata\n");
1826 		goto err;
1827 	}
1828 
1829 	return;
1830 
1831 err:
1832 	atomisp_css_free_stat_buffers(asd);
1833 	return;
1834 }
1835 
1836 static void atomisp_curr_user_grid_info(struct atomisp_sub_device *asd,
1837 					struct atomisp_grid_info *info)
1838 {
1839 	memcpy(info, &asd->params.curr_grid_info.s3a_grid,
1840 	       sizeof(struct ia_css_3a_grid_info));
1841 }
1842 
1843 int atomisp_compare_grid(struct atomisp_sub_device *asd,
1844 			 struct atomisp_grid_info *atomgrid)
1845 {
1846 	struct atomisp_grid_info tmp = {0};
1847 
1848 	atomisp_curr_user_grid_info(asd, &tmp);
1849 	return memcmp(atomgrid, &tmp, sizeof(tmp));
1850 }
1851 
1852 /*
1853  * Function to update Gdc table for gdc
1854  */
1855 int atomisp_gdc_cac_table(struct atomisp_sub_device *asd, int flag,
1856 			  struct atomisp_morph_table *config)
1857 {
1858 	int ret;
1859 	int i;
1860 	struct atomisp_device *isp = asd->isp;
1861 
1862 	if (flag == 0) {
1863 		/* Get gdc table from current setup */
1864 		struct ia_css_morph_table tab = {0};
1865 
1866 		atomisp_css_get_morph_table(asd, &tab);
1867 
1868 		config->width = tab.width;
1869 		config->height = tab.height;
1870 
1871 		for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
1872 			ret = copy_to_user(config->coordinates_x[i],
1873 					   tab.coordinates_x[i], tab.height *
1874 					   tab.width * sizeof(*tab.coordinates_x[i]));
1875 			if (ret) {
1876 				dev_err(isp->dev,
1877 					"Failed to copy to User for x\n");
1878 				return -EFAULT;
1879 			}
1880 			ret = copy_to_user(config->coordinates_y[i],
1881 					   tab.coordinates_y[i], tab.height *
1882 					   tab.width * sizeof(*tab.coordinates_y[i]));
1883 			if (ret) {
1884 				dev_err(isp->dev,
1885 					"Failed to copy to User for y\n");
1886 				return -EFAULT;
1887 			}
1888 		}
1889 	} else {
1890 		struct ia_css_morph_table *tab =
1891 			    asd->params.css_param.morph_table;
1892 
1893 		/* free first if we have one */
1894 		if (tab) {
1895 			atomisp_css_morph_table_free(tab);
1896 			asd->params.css_param.morph_table = NULL;
1897 		}
1898 
1899 		/* allocate new one */
1900 		tab = atomisp_css_morph_table_allocate(config->width,
1901 						       config->height);
1902 
1903 		if (!tab) {
1904 			dev_err(isp->dev, "out of memory\n");
1905 			return -EINVAL;
1906 		}
1907 
1908 		for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
1909 			ret = copy_from_user(tab->coordinates_x[i],
1910 					     config->coordinates_x[i],
1911 					     config->height * config->width *
1912 					     sizeof(*config->coordinates_x[i]));
1913 			if (ret) {
1914 				dev_err(isp->dev,
1915 					"Failed to copy from User for x, ret %d\n",
1916 					ret);
1917 				atomisp_css_morph_table_free(tab);
1918 				return -EFAULT;
1919 			}
1920 			ret = copy_from_user(tab->coordinates_y[i],
1921 					     config->coordinates_y[i],
1922 					     config->height * config->width *
1923 					     sizeof(*config->coordinates_y[i]));
1924 			if (ret) {
1925 				dev_err(isp->dev,
1926 					"Failed to copy from User for y, ret is %d\n",
1927 					ret);
1928 				atomisp_css_morph_table_free(tab);
1929 				return -EFAULT;
1930 			}
1931 		}
1932 		asd->params.css_param.morph_table = tab;
1933 		if (asd->params.gdc_cac_en)
1934 			asd->params.config.morph_table = tab;
1935 	}
1936 
1937 	return 0;
1938 }
1939 
1940 int atomisp_macc_table(struct atomisp_sub_device *asd, int flag,
1941 		       struct atomisp_macc_config *config)
1942 {
1943 	struct ia_css_macc_table *macc_table;
1944 
1945 	switch (config->color_effect) {
1946 	case V4L2_COLORFX_NONE:
1947 		macc_table = &asd->params.css_param.macc_table;
1948 		break;
1949 	case V4L2_COLORFX_SKY_BLUE:
1950 		macc_table = &blue_macc_table;
1951 		break;
1952 	case V4L2_COLORFX_GRASS_GREEN:
1953 		macc_table = &green_macc_table;
1954 		break;
1955 	case V4L2_COLORFX_SKIN_WHITEN_LOW:
1956 		macc_table = &skin_low_macc_table;
1957 		break;
1958 	case V4L2_COLORFX_SKIN_WHITEN:
1959 		macc_table = &skin_medium_macc_table;
1960 		break;
1961 	case V4L2_COLORFX_SKIN_WHITEN_HIGH:
1962 		macc_table = &skin_high_macc_table;
1963 		break;
1964 	default:
1965 		return -EINVAL;
1966 	}
1967 
1968 	if (flag == 0) {
1969 		/* Get macc table from current setup */
1970 		memcpy(&config->table, macc_table,
1971 		       sizeof(struct ia_css_macc_table));
1972 	} else {
1973 		memcpy(macc_table, &config->table,
1974 		       sizeof(struct ia_css_macc_table));
1975 		if (config->color_effect == asd->params.color_effect)
1976 			asd->params.config.macc_table = macc_table;
1977 	}
1978 
1979 	return 0;
1980 }
1981 
1982 int atomisp_set_dis_vector(struct atomisp_sub_device *asd,
1983 			   struct atomisp_dis_vector *vector)
1984 {
1985 	atomisp_css_video_set_dis_vector(asd, vector);
1986 
1987 	asd->params.dis_proj_data_valid = false;
1988 	asd->params.css_update_params_needed = true;
1989 	return 0;
1990 }
1991 
1992 /*
1993  * Function to set/get image stablization statistics
1994  */
1995 int atomisp_get_dis_stat(struct atomisp_sub_device *asd,
1996 			 struct atomisp_dis_statistics *stats)
1997 {
1998 	return atomisp_css_get_dis_stat(asd, stats);
1999 }
2000 
2001 /*
2002  * Function  set camrea_prefiles.xml current sensor pixel array size
2003  */
2004 int atomisp_set_array_res(struct atomisp_sub_device *asd,
2005 			  struct atomisp_resolution  *config)
2006 {
2007 	dev_dbg(asd->isp->dev, ">%s start\n", __func__);
2008 	if (!config) {
2009 		dev_err(asd->isp->dev, "Set sensor array size is not valid\n");
2010 		return -EINVAL;
2011 	}
2012 
2013 	asd->sensor_array_res.width = config->width;
2014 	asd->sensor_array_res.height = config->height;
2015 	return 0;
2016 }
2017 
2018 /*
2019  * Function to get DVS2 BQ resolution settings
2020  */
2021 int atomisp_get_dvs2_bq_resolutions(struct atomisp_sub_device *asd,
2022 				    struct atomisp_dvs2_bq_resolutions *bq_res)
2023 {
2024 	struct ia_css_pipe_config *pipe_cfg = NULL;
2025 	struct ia_css_stream_config *stream_cfg = NULL;
2026 	struct ia_css_stream_input_config *input_config = NULL;
2027 
2028 	struct ia_css_stream *stream =
2029 		    asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
2030 	if (!stream) {
2031 		dev_warn(asd->isp->dev, "stream is not created");
2032 		return -EAGAIN;
2033 	}
2034 
2035 	pipe_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
2036 		   .pipe_configs[IA_CSS_PIPE_ID_VIDEO];
2037 	stream_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
2038 		     .stream_config;
2039 	input_config = &stream_cfg->input_config;
2040 
2041 	if (!bq_res)
2042 		return -EINVAL;
2043 
2044 	/* the GDC output resolution */
2045 	bq_res->output_bq.width_bq = pipe_cfg->output_info[0].res.width / 2;
2046 	bq_res->output_bq.height_bq = pipe_cfg->output_info[0].res.height / 2;
2047 
2048 	bq_res->envelope_bq.width_bq = 0;
2049 	bq_res->envelope_bq.height_bq = 0;
2050 	/* the GDC input resolution */
2051 	if (!asd->continuous_mode->val) {
2052 		bq_res->source_bq.width_bq = bq_res->output_bq.width_bq +
2053 					     pipe_cfg->dvs_envelope.width / 2;
2054 		bq_res->source_bq.height_bq = bq_res->output_bq.height_bq +
2055 					      pipe_cfg->dvs_envelope.height / 2;
2056 		/*
2057 		 * Bad pixels caused by spatial filter processing
2058 		 * ISP filter resolution should be given by CSS/FW, but for now
2059 		 * there is not such API to query, and it is fixed value, so
2060 		 * hardcoded here.
2061 		 */
2062 		bq_res->ispfilter_bq.width_bq = 12 / 2;
2063 		bq_res->ispfilter_bq.height_bq = 12 / 2;
2064 		/* spatial filter shift, always 4 pixels */
2065 		bq_res->gdc_shift_bq.width_bq = 4 / 2;
2066 		bq_res->gdc_shift_bq.height_bq = 4 / 2;
2067 
2068 		if (asd->params.video_dis_en) {
2069 			bq_res->envelope_bq.width_bq = pipe_cfg->dvs_envelope.width
2070 						       / 2 - bq_res->ispfilter_bq.width_bq;
2071 			bq_res->envelope_bq.height_bq = pipe_cfg->dvs_envelope.height
2072 							/ 2 - bq_res->ispfilter_bq.height_bq;
2073 		}
2074 	} else {
2075 		unsigned int w_padding;
2076 		unsigned int gdc_effective_input = 0;
2077 
2078 		/* For GDC:
2079 		 * gdc_effective_input = effective_input + envelope
2080 		 *
2081 		 * From the comment and formula in BZ1786,
2082 		 * we see the source_bq should be:
2083 		 * effective_input / bayer_ds_ratio
2084 		 */
2085 		bq_res->source_bq.width_bq =
2086 		    (input_config->effective_res.width *
2087 		     pipe_cfg->bayer_ds_out_res.width /
2088 		     input_config->effective_res.width + 1) / 2;
2089 		bq_res->source_bq.height_bq =
2090 		    (input_config->effective_res.height *
2091 		     pipe_cfg->bayer_ds_out_res.height /
2092 		     input_config->effective_res.height + 1) / 2;
2093 
2094 		if (!asd->params.video_dis_en) {
2095 			/*
2096 			 * We adjust the ispfilter_bq to:
2097 			 * ispfilter_bq = 128/BDS
2098 			 * we still need firmware team to provide an offical
2099 			 * formula for SDV.
2100 			 */
2101 			bq_res->ispfilter_bq.width_bq = 128 *
2102 							pipe_cfg->bayer_ds_out_res.width /
2103 							input_config->effective_res.width / 2;
2104 			bq_res->ispfilter_bq.height_bq = 128 *
2105 							 pipe_cfg->bayer_ds_out_res.width /
2106 							 input_config->effective_res.width / 2;
2107 
2108 			if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401)) {
2109 				/* No additional left padding for ISYS2401 */
2110 				bq_res->gdc_shift_bq.width_bq = 4 / 2;
2111 				bq_res->gdc_shift_bq.height_bq = 4 / 2;
2112 			} else {
2113 				/*
2114 				 * For the w_padding and gdc_shift_bq cacluation
2115 				 * Please see the BZ 1786 and 4358 for more info.
2116 				 * Just test that this formula can work now,
2117 				 * but we still have no offical formula.
2118 				 *
2119 				 * w_padding = ceiling(gdc_effective_input
2120 				 *             /128, 1) * 128 - effective_width
2121 				 * gdc_shift_bq = w_padding/BDS/2 + ispfilter_bq/2
2122 				 */
2123 				gdc_effective_input =
2124 				    input_config->effective_res.width +
2125 				    pipe_cfg->dvs_envelope.width;
2126 				w_padding = roundup(gdc_effective_input, 128) -
2127 					    input_config->effective_res.width;
2128 				w_padding = w_padding *
2129 					    pipe_cfg->bayer_ds_out_res.width /
2130 					    input_config->effective_res.width + 1;
2131 				w_padding = roundup(w_padding / 2, 1);
2132 
2133 				bq_res->gdc_shift_bq.width_bq = bq_res->ispfilter_bq.width_bq / 2
2134 								+ w_padding;
2135 				bq_res->gdc_shift_bq.height_bq = 4 / 2;
2136 			}
2137 		} else {
2138 			unsigned int dvs_w, dvs_h, dvs_w_max, dvs_h_max;
2139 
2140 			bq_res->ispfilter_bq.width_bq = 8 / 2;
2141 			bq_res->ispfilter_bq.height_bq = 8 / 2;
2142 
2143 			if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401)) {
2144 				/* No additional left padding for ISYS2401 */
2145 				bq_res->gdc_shift_bq.width_bq = 4 / 2;
2146 				bq_res->gdc_shift_bq.height_bq = 4 / 2;
2147 			} else {
2148 				w_padding =
2149 				    roundup(input_config->effective_res.width, 128) -
2150 				    input_config->effective_res.width;
2151 				if (w_padding < 12)
2152 					w_padding = 12;
2153 				bq_res->gdc_shift_bq.width_bq = 4 / 2 +
2154 								((w_padding - 12) *
2155 								 pipe_cfg->bayer_ds_out_res.width /
2156 								 input_config->effective_res.width + 1) / 2;
2157 				bq_res->gdc_shift_bq.height_bq = 4 / 2;
2158 			}
2159 
2160 			dvs_w = pipe_cfg->bayer_ds_out_res.width -
2161 				pipe_cfg->output_info[0].res.width;
2162 			dvs_h = pipe_cfg->bayer_ds_out_res.height -
2163 				pipe_cfg->output_info[0].res.height;
2164 			dvs_w_max = rounddown(
2165 					pipe_cfg->output_info[0].res.width / 5,
2166 					ATOM_ISP_STEP_WIDTH);
2167 			dvs_h_max = rounddown(
2168 					pipe_cfg->output_info[0].res.height / 5,
2169 					ATOM_ISP_STEP_HEIGHT);
2170 			bq_res->envelope_bq.width_bq =
2171 			    min((dvs_w / 2), (dvs_w_max / 2)) -
2172 			    bq_res->ispfilter_bq.width_bq;
2173 			bq_res->envelope_bq.height_bq =
2174 			    min((dvs_h / 2), (dvs_h_max / 2)) -
2175 			    bq_res->ispfilter_bq.height_bq;
2176 		}
2177 	}
2178 
2179 	dev_dbg(asd->isp->dev,
2180 		"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",
2181 		bq_res->source_bq.width_bq, bq_res->source_bq.height_bq,
2182 		bq_res->ispfilter_bq.width_bq, bq_res->ispfilter_bq.height_bq,
2183 		bq_res->gdc_shift_bq.width_bq, bq_res->gdc_shift_bq.height_bq,
2184 		bq_res->envelope_bq.width_bq, bq_res->envelope_bq.height_bq,
2185 		bq_res->output_bq.width_bq, bq_res->output_bq.height_bq);
2186 
2187 	return 0;
2188 }
2189 
2190 int atomisp_set_dis_coefs(struct atomisp_sub_device *asd,
2191 			  struct atomisp_dis_coefficients *coefs)
2192 {
2193 	return atomisp_css_set_dis_coefs(asd, coefs);
2194 }
2195 
2196 /*
2197  * Function to set/get 3A stat from isp
2198  */
2199 int atomisp_3a_stat(struct atomisp_sub_device *asd, int flag,
2200 		    struct atomisp_3a_statistics *config)
2201 {
2202 	struct atomisp_device *isp = asd->isp;
2203 	struct atomisp_s3a_buf *s3a_buf;
2204 	unsigned long ret;
2205 
2206 	if (flag != 0)
2207 		return -EINVAL;
2208 
2209 	/* sanity check to avoid writing into unallocated memory. */
2210 	if (asd->params.s3a_output_bytes == 0)
2211 		return -EINVAL;
2212 
2213 	if (atomisp_compare_grid(asd, &config->grid_info) != 0) {
2214 		/* If the grid info in the argument differs from the current
2215 		   grid info, we tell the caller to reset the grid size and
2216 		   try again. */
2217 		return -EAGAIN;
2218 	}
2219 
2220 	if (list_empty(&asd->s3a_stats_ready)) {
2221 		dev_err(isp->dev, "3a statistics is not valid.\n");
2222 		return -EAGAIN;
2223 	}
2224 
2225 	s3a_buf = list_entry(asd->s3a_stats_ready.next,
2226 			     struct atomisp_s3a_buf, list);
2227 	if (s3a_buf->s3a_map)
2228 		ia_css_translate_3a_statistics(
2229 		    asd->params.s3a_user_stat, s3a_buf->s3a_map);
2230 	else
2231 		ia_css_get_3a_statistics(asd->params.s3a_user_stat,
2232 					 s3a_buf->s3a_data);
2233 
2234 	config->exp_id = s3a_buf->s3a_data->exp_id;
2235 	config->isp_config_id = s3a_buf->s3a_data->isp_config_id;
2236 
2237 	ret = copy_to_user(config->data, asd->params.s3a_user_stat->data,
2238 			   asd->params.s3a_output_bytes);
2239 	if (ret) {
2240 		dev_err(isp->dev, "copy to user failed: copied %lu bytes\n",
2241 			ret);
2242 		return -EFAULT;
2243 	}
2244 
2245 	/* Move to free buffer list */
2246 	list_del_init(&s3a_buf->list);
2247 	list_add_tail(&s3a_buf->list, &asd->s3a_stats);
2248 	dev_dbg(isp->dev, "%s: finish getting exp_id %d 3a stat, isp_config_id %d\n",
2249 		__func__,
2250 		config->exp_id, config->isp_config_id);
2251 	return 0;
2252 }
2253 
2254 int atomisp_get_metadata(struct atomisp_sub_device *asd, int flag,
2255 			 struct atomisp_metadata *md)
2256 {
2257 	struct atomisp_device *isp = asd->isp;
2258 	struct ia_css_stream_info *stream_info;
2259 	struct camera_mipi_info *mipi_info;
2260 	struct atomisp_metadata_buf *md_buf;
2261 	enum atomisp_metadata_type md_type = ATOMISP_MAIN_METADATA;
2262 	int ret, i;
2263 
2264 	if (flag != 0)
2265 		return -EINVAL;
2266 
2267 	stream_info = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2268 		      stream_info;
2269 
2270 	/* We always return the resolution and stride even if there is
2271 	 * no valid metadata. This allows the caller to get the information
2272 	 * needed to allocate user-space buffers. */
2273 	md->width  = stream_info->metadata_info.resolution.width;
2274 	md->height = stream_info->metadata_info.resolution.height;
2275 	md->stride = stream_info->metadata_info.stride;
2276 
2277 	/* sanity check to avoid writing into unallocated memory.
2278 	 * This does not return an error because it is a valid way
2279 	 * for applications to detect that metadata is not enabled. */
2280 	if (md->width == 0 || md->height == 0 || !md->data)
2281 		return 0;
2282 
2283 	/* This is done in the atomisp_buf_done() */
2284 	if (list_empty(&asd->metadata_ready[md_type])) {
2285 		dev_warn(isp->dev, "Metadata queue is empty now!\n");
2286 		return -EAGAIN;
2287 	}
2288 
2289 	mipi_info = atomisp_to_sensor_mipi_info(
2290 			isp->inputs[asd->input_curr].camera);
2291 	if (!mipi_info)
2292 		return -EINVAL;
2293 
2294 	if (mipi_info->metadata_effective_width) {
2295 		for (i = 0; i < md->height; i++)
2296 			md->effective_width[i] =
2297 			    mipi_info->metadata_effective_width[i];
2298 	}
2299 
2300 	md_buf = list_entry(asd->metadata_ready[md_type].next,
2301 			    struct atomisp_metadata_buf, list);
2302 	md->exp_id = md_buf->metadata->exp_id;
2303 	if (md_buf->md_vptr) {
2304 		ret = copy_to_user(md->data,
2305 				   md_buf->md_vptr,
2306 				   stream_info->metadata_info.size);
2307 	} else {
2308 		hmm_load(md_buf->metadata->address,
2309 			 asd->params.metadata_user[md_type],
2310 			 stream_info->metadata_info.size);
2311 
2312 		ret = copy_to_user(md->data,
2313 				   asd->params.metadata_user[md_type],
2314 				   stream_info->metadata_info.size);
2315 	}
2316 	if (ret) {
2317 		dev_err(isp->dev, "copy to user failed: copied %d bytes\n",
2318 			ret);
2319 		return -EFAULT;
2320 	}
2321 
2322 	list_del_init(&md_buf->list);
2323 	list_add_tail(&md_buf->list, &asd->metadata[md_type]);
2324 
2325 	dev_dbg(isp->dev, "%s: HAL de-queued metadata type %d with exp_id %d\n",
2326 		__func__, md_type, md->exp_id);
2327 	return 0;
2328 }
2329 
2330 int atomisp_get_metadata_by_type(struct atomisp_sub_device *asd, int flag,
2331 				 struct atomisp_metadata_with_type *md)
2332 {
2333 	struct atomisp_device *isp = asd->isp;
2334 	struct ia_css_stream_info *stream_info;
2335 	struct camera_mipi_info *mipi_info;
2336 	struct atomisp_metadata_buf *md_buf;
2337 	enum atomisp_metadata_type md_type;
2338 	int ret, i;
2339 
2340 	if (flag != 0)
2341 		return -EINVAL;
2342 
2343 	stream_info = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2344 		      stream_info;
2345 
2346 	/* We always return the resolution and stride even if there is
2347 	 * no valid metadata. This allows the caller to get the information
2348 	 * needed to allocate user-space buffers. */
2349 	md->width  = stream_info->metadata_info.resolution.width;
2350 	md->height = stream_info->metadata_info.resolution.height;
2351 	md->stride = stream_info->metadata_info.stride;
2352 
2353 	/* sanity check to avoid writing into unallocated memory.
2354 	 * This does not return an error because it is a valid way
2355 	 * for applications to detect that metadata is not enabled. */
2356 	if (md->width == 0 || md->height == 0 || !md->data)
2357 		return 0;
2358 
2359 	md_type = md->type;
2360 	if (md_type < 0 || md_type >= ATOMISP_METADATA_TYPE_NUM)
2361 		return -EINVAL;
2362 
2363 	/* This is done in the atomisp_buf_done() */
2364 	if (list_empty(&asd->metadata_ready[md_type])) {
2365 		dev_warn(isp->dev, "Metadata queue is empty now!\n");
2366 		return -EAGAIN;
2367 	}
2368 
2369 	mipi_info = atomisp_to_sensor_mipi_info(
2370 			isp->inputs[asd->input_curr].camera);
2371 	if (!mipi_info)
2372 		return -EINVAL;
2373 
2374 	if (mipi_info->metadata_effective_width) {
2375 		for (i = 0; i < md->height; i++)
2376 			md->effective_width[i] =
2377 			    mipi_info->metadata_effective_width[i];
2378 	}
2379 
2380 	md_buf = list_entry(asd->metadata_ready[md_type].next,
2381 			    struct atomisp_metadata_buf, list);
2382 	md->exp_id = md_buf->metadata->exp_id;
2383 	if (md_buf->md_vptr) {
2384 		ret = copy_to_user(md->data,
2385 				   md_buf->md_vptr,
2386 				   stream_info->metadata_info.size);
2387 	} else {
2388 		hmm_load(md_buf->metadata->address,
2389 			 asd->params.metadata_user[md_type],
2390 			 stream_info->metadata_info.size);
2391 
2392 		ret = copy_to_user(md->data,
2393 				   asd->params.metadata_user[md_type],
2394 				   stream_info->metadata_info.size);
2395 	}
2396 	if (ret) {
2397 		dev_err(isp->dev, "copy to user failed: copied %d bytes\n",
2398 			ret);
2399 		return -EFAULT;
2400 	} else {
2401 		list_del_init(&md_buf->list);
2402 		list_add_tail(&md_buf->list, &asd->metadata[md_type]);
2403 	}
2404 	dev_dbg(isp->dev, "%s: HAL de-queued metadata type %d with exp_id %d\n",
2405 		__func__, md_type, md->exp_id);
2406 	return 0;
2407 }
2408 
2409 /*
2410  * Function to calculate real zoom region for every pipe
2411  */
2412 int atomisp_calculate_real_zoom_region(struct atomisp_sub_device *asd,
2413 				       struct ia_css_dz_config   *dz_config,
2414 				       enum ia_css_pipe_id css_pipe_id)
2415 
2416 {
2417 	struct atomisp_stream_env *stream_env =
2418 		    &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL];
2419 	struct atomisp_resolution  eff_res, out_res;
2420 	int w_offset, h_offset;
2421 
2422 	memset(&eff_res, 0, sizeof(eff_res));
2423 	memset(&out_res, 0, sizeof(out_res));
2424 
2425 	if (dz_config->dx || dz_config->dy)
2426 		return 0;
2427 
2428 	if (css_pipe_id != IA_CSS_PIPE_ID_PREVIEW
2429 	    && css_pipe_id != IA_CSS_PIPE_ID_CAPTURE) {
2430 		dev_err(asd->isp->dev, "%s the set pipe no support crop region"
2431 			, __func__);
2432 		return -EINVAL;
2433 	}
2434 
2435 	eff_res.width =
2436 	    stream_env->stream_config.input_config.effective_res.width;
2437 	eff_res.height =
2438 	    stream_env->stream_config.input_config.effective_res.height;
2439 	if (eff_res.width == 0 || eff_res.height == 0) {
2440 		dev_err(asd->isp->dev, "%s err effective resolution"
2441 			, __func__);
2442 		return -EINVAL;
2443 	}
2444 
2445 	if (dz_config->zoom_region.resolution.width
2446 	    == asd->sensor_array_res.width
2447 	    || dz_config->zoom_region.resolution.height
2448 	    == asd->sensor_array_res.height) {
2449 		/*no need crop region*/
2450 		dz_config->zoom_region.origin.x = 0;
2451 		dz_config->zoom_region.origin.y = 0;
2452 		dz_config->zoom_region.resolution.width = eff_res.width;
2453 		dz_config->zoom_region.resolution.height = eff_res.height;
2454 		return 0;
2455 	}
2456 
2457 	/* FIXME:
2458 	 * This is not the correct implementation with Google's definition, due
2459 	 * to firmware limitation.
2460 	 * map real crop region base on above calculating base max crop region.
2461 	 */
2462 
2463 	if (!IS_ISP2401) {
2464 		dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
2465 						  * eff_res.width
2466 						  / asd->sensor_array_res.width;
2467 		dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
2468 						  * eff_res.height
2469 						  / asd->sensor_array_res.height;
2470 		dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
2471 							  * eff_res.width
2472 							  / asd->sensor_array_res.width;
2473 		dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
2474 							  * eff_res.height
2475 							  / asd->sensor_array_res.height;
2476 		/*
2477 		 * Set same ratio of crop region resolution and current pipe output
2478 		 * resolution
2479 		 */
2480 		out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
2481 		out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
2482 		if (out_res.width == 0 || out_res.height == 0) {
2483 			dev_err(asd->isp->dev, "%s err current pipe output resolution"
2484 				, __func__);
2485 			return -EINVAL;
2486 		}
2487 	} else {
2488 		out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
2489 		out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
2490 		if (out_res.width == 0 || out_res.height == 0) {
2491 			dev_err(asd->isp->dev, "%s err current pipe output resolution"
2492 				, __func__);
2493 			return -EINVAL;
2494 		}
2495 
2496 		if (asd->sensor_array_res.width * out_res.height
2497 		    < out_res.width * asd->sensor_array_res.height) {
2498 			h_offset = asd->sensor_array_res.height
2499 				   - asd->sensor_array_res.width
2500 				   * out_res.height / out_res.width;
2501 			h_offset = h_offset / 2;
2502 			if (dz_config->zoom_region.origin.y < h_offset)
2503 				dz_config->zoom_region.origin.y = 0;
2504 			else
2505 				dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y - h_offset;
2506 			w_offset = 0;
2507 		} else {
2508 			w_offset = asd->sensor_array_res.width
2509 				   - asd->sensor_array_res.height
2510 				   * out_res.width / out_res.height;
2511 			w_offset = w_offset / 2;
2512 			if (dz_config->zoom_region.origin.x < w_offset)
2513 				dz_config->zoom_region.origin.x = 0;
2514 			else
2515 				dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x - w_offset;
2516 			h_offset = 0;
2517 		}
2518 		dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
2519 						  * eff_res.width
2520 						  / (asd->sensor_array_res.width - 2 * w_offset);
2521 		dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
2522 						  * eff_res.height
2523 						  / (asd->sensor_array_res.height - 2 * h_offset);
2524 		dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
2525 						  * eff_res.width
2526 						  / (asd->sensor_array_res.width - 2 * w_offset);
2527 		dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
2528 						  * eff_res.height
2529 						  / (asd->sensor_array_res.height - 2 * h_offset);
2530 	}
2531 
2532 	if (out_res.width * dz_config->zoom_region.resolution.height
2533 	    > dz_config->zoom_region.resolution.width * out_res.height) {
2534 		dz_config->zoom_region.resolution.height =
2535 		    dz_config->zoom_region.resolution.width
2536 		    * out_res.height / out_res.width;
2537 	} else {
2538 		dz_config->zoom_region.resolution.width =
2539 		    dz_config->zoom_region.resolution.height
2540 		    * out_res.width / out_res.height;
2541 	}
2542 	dev_dbg(asd->isp->dev,
2543 		"%s crop region:(%d,%d),(%d,%d) eff_res(%d, %d) array_size(%d,%d) out_res(%d, %d)\n",
2544 		__func__, dz_config->zoom_region.origin.x,
2545 		dz_config->zoom_region.origin.y,
2546 		dz_config->zoom_region.resolution.width,
2547 		dz_config->zoom_region.resolution.height,
2548 		eff_res.width, eff_res.height,
2549 		asd->sensor_array_res.width,
2550 		asd->sensor_array_res.height,
2551 		out_res.width, out_res.height);
2552 
2553 	if ((dz_config->zoom_region.origin.x +
2554 	     dz_config->zoom_region.resolution.width
2555 	     > eff_res.width) ||
2556 	    (dz_config->zoom_region.origin.y +
2557 	     dz_config->zoom_region.resolution.height
2558 	     > eff_res.height))
2559 		return -EINVAL;
2560 
2561 	return 0;
2562 }
2563 
2564 /*
2565  * Function to check the zoom region whether is effective
2566  */
2567 static bool atomisp_check_zoom_region(
2568     struct atomisp_sub_device *asd,
2569     struct ia_css_dz_config *dz_config)
2570 {
2571 	struct atomisp_resolution  config;
2572 	bool flag = false;
2573 	unsigned int w, h;
2574 
2575 	memset(&config, 0, sizeof(struct atomisp_resolution));
2576 
2577 	if (dz_config->dx && dz_config->dy)
2578 		return true;
2579 
2580 	config.width = asd->sensor_array_res.width;
2581 	config.height = asd->sensor_array_res.height;
2582 	w = dz_config->zoom_region.origin.x +
2583 	    dz_config->zoom_region.resolution.width;
2584 	h = dz_config->zoom_region.origin.y +
2585 	    dz_config->zoom_region.resolution.height;
2586 
2587 	if ((w <= config.width) && (h <= config.height) && w > 0 && h > 0)
2588 		flag = true;
2589 	else
2590 		/* setting error zoom region */
2591 		dev_err(asd->isp->dev,
2592 			"%s zoom region ERROR:dz_config:(%d,%d),(%d,%d)array_res(%d, %d)\n",
2593 			__func__, dz_config->zoom_region.origin.x,
2594 			dz_config->zoom_region.origin.y,
2595 			dz_config->zoom_region.resolution.width,
2596 			dz_config->zoom_region.resolution.height,
2597 			config.width, config.height);
2598 
2599 	return flag;
2600 }
2601 
2602 void atomisp_apply_css_parameters(
2603     struct atomisp_sub_device *asd,
2604     struct atomisp_css_params *css_param)
2605 {
2606 	if (css_param->update_flag.wb_config)
2607 		asd->params.config.wb_config = &css_param->wb_config;
2608 
2609 	if (css_param->update_flag.ob_config)
2610 		asd->params.config.ob_config = &css_param->ob_config;
2611 
2612 	if (css_param->update_flag.dp_config)
2613 		asd->params.config.dp_config = &css_param->dp_config;
2614 
2615 	if (css_param->update_flag.nr_config)
2616 		asd->params.config.nr_config = &css_param->nr_config;
2617 
2618 	if (css_param->update_flag.ee_config)
2619 		asd->params.config.ee_config = &css_param->ee_config;
2620 
2621 	if (css_param->update_flag.tnr_config)
2622 		asd->params.config.tnr_config = &css_param->tnr_config;
2623 
2624 	if (css_param->update_flag.a3a_config)
2625 		asd->params.config.s3a_config = &css_param->s3a_config;
2626 
2627 	if (css_param->update_flag.ctc_config)
2628 		asd->params.config.ctc_config = &css_param->ctc_config;
2629 
2630 	if (css_param->update_flag.cnr_config)
2631 		asd->params.config.cnr_config = &css_param->cnr_config;
2632 
2633 	if (css_param->update_flag.ecd_config)
2634 		asd->params.config.ecd_config = &css_param->ecd_config;
2635 
2636 	if (css_param->update_flag.ynr_config)
2637 		asd->params.config.ynr_config = &css_param->ynr_config;
2638 
2639 	if (css_param->update_flag.fc_config)
2640 		asd->params.config.fc_config = &css_param->fc_config;
2641 
2642 	if (css_param->update_flag.macc_config)
2643 		asd->params.config.macc_config = &css_param->macc_config;
2644 
2645 	if (css_param->update_flag.aa_config)
2646 		asd->params.config.aa_config = &css_param->aa_config;
2647 
2648 	if (css_param->update_flag.anr_config)
2649 		asd->params.config.anr_config = &css_param->anr_config;
2650 
2651 	if (css_param->update_flag.xnr_config)
2652 		asd->params.config.xnr_config = &css_param->xnr_config;
2653 
2654 	if (css_param->update_flag.yuv2rgb_cc_config)
2655 		asd->params.config.yuv2rgb_cc_config = &css_param->yuv2rgb_cc_config;
2656 
2657 	if (css_param->update_flag.rgb2yuv_cc_config)
2658 		asd->params.config.rgb2yuv_cc_config = &css_param->rgb2yuv_cc_config;
2659 
2660 	if (css_param->update_flag.macc_table)
2661 		asd->params.config.macc_table = &css_param->macc_table;
2662 
2663 	if (css_param->update_flag.xnr_table)
2664 		asd->params.config.xnr_table = &css_param->xnr_table;
2665 
2666 	if (css_param->update_flag.r_gamma_table)
2667 		asd->params.config.r_gamma_table = &css_param->r_gamma_table;
2668 
2669 	if (css_param->update_flag.g_gamma_table)
2670 		asd->params.config.g_gamma_table = &css_param->g_gamma_table;
2671 
2672 	if (css_param->update_flag.b_gamma_table)
2673 		asd->params.config.b_gamma_table = &css_param->b_gamma_table;
2674 
2675 	if (css_param->update_flag.anr_thres)
2676 		atomisp_css_set_anr_thres(asd, &css_param->anr_thres);
2677 
2678 	if (css_param->update_flag.shading_table)
2679 		asd->params.config.shading_table = css_param->shading_table;
2680 
2681 	if (css_param->update_flag.morph_table && asd->params.gdc_cac_en)
2682 		asd->params.config.morph_table = css_param->morph_table;
2683 
2684 	if (css_param->update_flag.dvs2_coefs) {
2685 		struct ia_css_dvs_grid_info *dvs_grid_info =
2686 		    atomisp_css_get_dvs_grid_info(
2687 			&asd->params.curr_grid_info);
2688 
2689 		if (dvs_grid_info && dvs_grid_info->enable)
2690 			atomisp_css_set_dvs2_coefs(asd, css_param->dvs2_coeff);
2691 	}
2692 
2693 	if (css_param->update_flag.dvs_6axis_config)
2694 		atomisp_css_set_dvs_6axis(asd, css_param->dvs_6axis);
2695 
2696 	atomisp_css_set_isp_config_id(asd, css_param->isp_config_id);
2697 	/*
2698 	 * These configurations are on used by ISP1.x, not for ISP2.x,
2699 	 * so do not handle them. see comments of ia_css_isp_config.
2700 	 * 1 cc_config
2701 	 * 2 ce_config
2702 	 * 3 de_config
2703 	 * 4 gc_config
2704 	 * 5 gamma_table
2705 	 * 6 ctc_table
2706 	 * 7 dvs_coefs
2707 	 */
2708 }
2709 
2710 static unsigned int long copy_from_compatible(void *to, const void *from,
2711 	unsigned long n, bool from_user)
2712 {
2713 	if (from_user)
2714 		return copy_from_user(to, (void __user *)from, n);
2715 	else
2716 		memcpy(to, from, n);
2717 	return 0;
2718 }
2719 
2720 int atomisp_cp_general_isp_parameters(struct atomisp_sub_device *asd,
2721 				      struct atomisp_parameters *arg,
2722 				      struct atomisp_css_params *css_param,
2723 				      bool from_user)
2724 {
2725 	struct atomisp_parameters *cur_config = &css_param->update_flag;
2726 
2727 	if (!arg || !asd || !css_param)
2728 		return -EINVAL;
2729 
2730 	if (arg->wb_config && (from_user || !cur_config->wb_config)) {
2731 		if (copy_from_compatible(&css_param->wb_config, arg->wb_config,
2732 					 sizeof(struct ia_css_wb_config),
2733 					 from_user))
2734 			return -EFAULT;
2735 		css_param->update_flag.wb_config =
2736 		    (struct atomisp_wb_config *)&css_param->wb_config;
2737 	}
2738 
2739 	if (arg->ob_config && (from_user || !cur_config->ob_config)) {
2740 		if (copy_from_compatible(&css_param->ob_config, arg->ob_config,
2741 					 sizeof(struct ia_css_ob_config),
2742 					 from_user))
2743 			return -EFAULT;
2744 		css_param->update_flag.ob_config =
2745 		    (struct atomisp_ob_config *)&css_param->ob_config;
2746 	}
2747 
2748 	if (arg->dp_config && (from_user || !cur_config->dp_config)) {
2749 		if (copy_from_compatible(&css_param->dp_config, arg->dp_config,
2750 					 sizeof(struct ia_css_dp_config),
2751 					 from_user))
2752 			return -EFAULT;
2753 		css_param->update_flag.dp_config =
2754 		    (struct atomisp_dp_config *)&css_param->dp_config;
2755 	}
2756 
2757 	if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
2758 		if (arg->dz_config && (from_user || !cur_config->dz_config)) {
2759 			if (copy_from_compatible(&css_param->dz_config,
2760 						 arg->dz_config,
2761 						 sizeof(struct ia_css_dz_config),
2762 						 from_user))
2763 				return -EFAULT;
2764 			if (!atomisp_check_zoom_region(asd,
2765 						       &css_param->dz_config)) {
2766 				dev_err(asd->isp->dev, "crop region error!");
2767 				return -EINVAL;
2768 			}
2769 			css_param->update_flag.dz_config =
2770 			    (struct atomisp_dz_config *)
2771 			    &css_param->dz_config;
2772 		}
2773 	}
2774 
2775 	if (arg->nr_config && (from_user || !cur_config->nr_config)) {
2776 		if (copy_from_compatible(&css_param->nr_config, arg->nr_config,
2777 					 sizeof(struct ia_css_nr_config),
2778 					 from_user))
2779 			return -EFAULT;
2780 		css_param->update_flag.nr_config =
2781 		    (struct atomisp_nr_config *)&css_param->nr_config;
2782 	}
2783 
2784 	if (arg->ee_config && (from_user || !cur_config->ee_config)) {
2785 		if (copy_from_compatible(&css_param->ee_config, arg->ee_config,
2786 					 sizeof(struct ia_css_ee_config),
2787 					 from_user))
2788 			return -EFAULT;
2789 		css_param->update_flag.ee_config =
2790 		    (struct atomisp_ee_config *)&css_param->ee_config;
2791 	}
2792 
2793 	if (arg->tnr_config && (from_user || !cur_config->tnr_config)) {
2794 		if (copy_from_compatible(&css_param->tnr_config,
2795 					 arg->tnr_config,
2796 					 sizeof(struct ia_css_tnr_config),
2797 					 from_user))
2798 			return -EFAULT;
2799 		css_param->update_flag.tnr_config =
2800 		    (struct atomisp_tnr_config *)
2801 		    &css_param->tnr_config;
2802 	}
2803 
2804 	if (arg->a3a_config && (from_user || !cur_config->a3a_config)) {
2805 		if (copy_from_compatible(&css_param->s3a_config,
2806 					 arg->a3a_config,
2807 					 sizeof(struct ia_css_3a_config),
2808 					 from_user))
2809 			return -EFAULT;
2810 		css_param->update_flag.a3a_config =
2811 		    (struct atomisp_3a_config *)&css_param->s3a_config;
2812 	}
2813 
2814 	if (arg->ctc_config && (from_user || !cur_config->ctc_config)) {
2815 		if (copy_from_compatible(&css_param->ctc_config,
2816 					 arg->ctc_config,
2817 					 sizeof(struct ia_css_ctc_config),
2818 					 from_user))
2819 			return -EFAULT;
2820 		css_param->update_flag.ctc_config =
2821 		    (struct atomisp_ctc_config *)
2822 		    &css_param->ctc_config;
2823 	}
2824 
2825 	if (arg->cnr_config && (from_user || !cur_config->cnr_config)) {
2826 		if (copy_from_compatible(&css_param->cnr_config,
2827 					 arg->cnr_config,
2828 					 sizeof(struct ia_css_cnr_config),
2829 					 from_user))
2830 			return -EFAULT;
2831 		css_param->update_flag.cnr_config =
2832 		    (struct atomisp_cnr_config *)
2833 		    &css_param->cnr_config;
2834 	}
2835 
2836 	if (arg->ecd_config && (from_user || !cur_config->ecd_config)) {
2837 		if (copy_from_compatible(&css_param->ecd_config,
2838 					 arg->ecd_config,
2839 					 sizeof(struct ia_css_ecd_config),
2840 					 from_user))
2841 			return -EFAULT;
2842 		css_param->update_flag.ecd_config =
2843 		    (struct atomisp_ecd_config *)
2844 		    &css_param->ecd_config;
2845 	}
2846 
2847 	if (arg->ynr_config && (from_user || !cur_config->ynr_config)) {
2848 		if (copy_from_compatible(&css_param->ynr_config,
2849 					 arg->ynr_config,
2850 					 sizeof(struct ia_css_ynr_config),
2851 					 from_user))
2852 			return -EFAULT;
2853 		css_param->update_flag.ynr_config =
2854 		    (struct atomisp_ynr_config *)
2855 		    &css_param->ynr_config;
2856 	}
2857 
2858 	if (arg->fc_config && (from_user || !cur_config->fc_config)) {
2859 		if (copy_from_compatible(&css_param->fc_config,
2860 					 arg->fc_config,
2861 					 sizeof(struct ia_css_fc_config),
2862 					 from_user))
2863 			return -EFAULT;
2864 		css_param->update_flag.fc_config =
2865 		    (struct atomisp_fc_config *)&css_param->fc_config;
2866 	}
2867 
2868 	if (arg->macc_config && (from_user || !cur_config->macc_config)) {
2869 		if (copy_from_compatible(&css_param->macc_config,
2870 					 arg->macc_config,
2871 					 sizeof(struct ia_css_macc_config),
2872 					 from_user))
2873 			return -EFAULT;
2874 		css_param->update_flag.macc_config =
2875 		    (struct atomisp_macc_config *)
2876 		    &css_param->macc_config;
2877 	}
2878 
2879 	if (arg->aa_config && (from_user || !cur_config->aa_config)) {
2880 		if (copy_from_compatible(&css_param->aa_config, arg->aa_config,
2881 					 sizeof(struct ia_css_aa_config),
2882 					 from_user))
2883 			return -EFAULT;
2884 		css_param->update_flag.aa_config =
2885 		    (struct atomisp_aa_config *)&css_param->aa_config;
2886 	}
2887 
2888 	if (arg->anr_config && (from_user || !cur_config->anr_config)) {
2889 		if (copy_from_compatible(&css_param->anr_config,
2890 					 arg->anr_config,
2891 					 sizeof(struct ia_css_anr_config),
2892 					 from_user))
2893 			return -EFAULT;
2894 		css_param->update_flag.anr_config =
2895 		    (struct atomisp_anr_config *)
2896 		    &css_param->anr_config;
2897 	}
2898 
2899 	if (arg->xnr_config && (from_user || !cur_config->xnr_config)) {
2900 		if (copy_from_compatible(&css_param->xnr_config,
2901 					 arg->xnr_config,
2902 					 sizeof(struct ia_css_xnr_config),
2903 					 from_user))
2904 			return -EFAULT;
2905 		css_param->update_flag.xnr_config =
2906 		    (struct atomisp_xnr_config *)
2907 		    &css_param->xnr_config;
2908 	}
2909 
2910 	if (arg->yuv2rgb_cc_config &&
2911 	    (from_user || !cur_config->yuv2rgb_cc_config)) {
2912 		if (copy_from_compatible(&css_param->yuv2rgb_cc_config,
2913 					 arg->yuv2rgb_cc_config,
2914 					 sizeof(struct ia_css_cc_config),
2915 					 from_user))
2916 			return -EFAULT;
2917 		css_param->update_flag.yuv2rgb_cc_config =
2918 		    (struct atomisp_cc_config *)
2919 		    &css_param->yuv2rgb_cc_config;
2920 	}
2921 
2922 	if (arg->rgb2yuv_cc_config &&
2923 	    (from_user || !cur_config->rgb2yuv_cc_config)) {
2924 		if (copy_from_compatible(&css_param->rgb2yuv_cc_config,
2925 					 arg->rgb2yuv_cc_config,
2926 					 sizeof(struct ia_css_cc_config),
2927 					 from_user))
2928 			return -EFAULT;
2929 		css_param->update_flag.rgb2yuv_cc_config =
2930 		    (struct atomisp_cc_config *)
2931 		    &css_param->rgb2yuv_cc_config;
2932 	}
2933 
2934 	if (arg->macc_table && (from_user || !cur_config->macc_table)) {
2935 		if (copy_from_compatible(&css_param->macc_table,
2936 					 arg->macc_table,
2937 					 sizeof(struct ia_css_macc_table),
2938 					 from_user))
2939 			return -EFAULT;
2940 		css_param->update_flag.macc_table =
2941 		    (struct atomisp_macc_table *)
2942 		    &css_param->macc_table;
2943 	}
2944 
2945 	if (arg->xnr_table && (from_user || !cur_config->xnr_table)) {
2946 		if (copy_from_compatible(&css_param->xnr_table,
2947 					 arg->xnr_table,
2948 					 sizeof(struct ia_css_xnr_table),
2949 					 from_user))
2950 			return -EFAULT;
2951 		css_param->update_flag.xnr_table =
2952 		    (struct atomisp_xnr_table *)&css_param->xnr_table;
2953 	}
2954 
2955 	if (arg->r_gamma_table && (from_user || !cur_config->r_gamma_table)) {
2956 		if (copy_from_compatible(&css_param->r_gamma_table,
2957 					 arg->r_gamma_table,
2958 					 sizeof(struct ia_css_rgb_gamma_table),
2959 					 from_user))
2960 			return -EFAULT;
2961 		css_param->update_flag.r_gamma_table =
2962 		    (struct atomisp_rgb_gamma_table *)
2963 		    &css_param->r_gamma_table;
2964 	}
2965 
2966 	if (arg->g_gamma_table && (from_user || !cur_config->g_gamma_table)) {
2967 		if (copy_from_compatible(&css_param->g_gamma_table,
2968 					 arg->g_gamma_table,
2969 					 sizeof(struct ia_css_rgb_gamma_table),
2970 					 from_user))
2971 			return -EFAULT;
2972 		css_param->update_flag.g_gamma_table =
2973 		    (struct atomisp_rgb_gamma_table *)
2974 		    &css_param->g_gamma_table;
2975 	}
2976 
2977 	if (arg->b_gamma_table && (from_user || !cur_config->b_gamma_table)) {
2978 		if (copy_from_compatible(&css_param->b_gamma_table,
2979 					 arg->b_gamma_table,
2980 					 sizeof(struct ia_css_rgb_gamma_table),
2981 					 from_user))
2982 			return -EFAULT;
2983 		css_param->update_flag.b_gamma_table =
2984 		    (struct atomisp_rgb_gamma_table *)
2985 		    &css_param->b_gamma_table;
2986 	}
2987 
2988 	if (arg->anr_thres && (from_user || !cur_config->anr_thres)) {
2989 		if (copy_from_compatible(&css_param->anr_thres, arg->anr_thres,
2990 					 sizeof(struct ia_css_anr_thres),
2991 					 from_user))
2992 			return -EFAULT;
2993 		css_param->update_flag.anr_thres =
2994 		    (struct atomisp_anr_thres *)&css_param->anr_thres;
2995 	}
2996 
2997 	if (from_user)
2998 		css_param->isp_config_id = arg->isp_config_id;
2999 	/*
3000 	 * These configurations are on used by ISP1.x, not for ISP2.x,
3001 	 * so do not handle them. see comments of ia_css_isp_config.
3002 	 * 1 cc_config
3003 	 * 2 ce_config
3004 	 * 3 de_config
3005 	 * 4 gc_config
3006 	 * 5 gamma_table
3007 	 * 6 ctc_table
3008 	 * 7 dvs_coefs
3009 	 */
3010 	return 0;
3011 }
3012 
3013 int atomisp_cp_lsc_table(struct atomisp_sub_device *asd,
3014 			 struct atomisp_shading_table *source_st,
3015 			 struct atomisp_css_params *css_param,
3016 			 bool from_user)
3017 {
3018 	unsigned int i;
3019 	unsigned int len_table;
3020 	struct ia_css_shading_table *shading_table;
3021 	struct ia_css_shading_table *old_table;
3022 	struct atomisp_shading_table *st, dest_st;
3023 
3024 	if (!source_st)
3025 		return 0;
3026 
3027 	if (!css_param)
3028 		return -EINVAL;
3029 
3030 	if (!from_user && css_param->update_flag.shading_table)
3031 		return 0;
3032 
3033 	if (IS_ISP2401) {
3034 		if (copy_from_compatible(&dest_st, source_st,
3035 					sizeof(struct atomisp_shading_table),
3036 					from_user)) {
3037 			dev_err(asd->isp->dev, "copy shading table failed!");
3038 			return -EFAULT;
3039 		}
3040 		st = &dest_st;
3041 	} else {
3042 		st = source_st;
3043 	}
3044 
3045 	old_table = css_param->shading_table;
3046 
3047 	/* user config is to disable the shading table. */
3048 	if (!st->enable) {
3049 		/* Generate a minimum table with enable = 0. */
3050 		shading_table = atomisp_css_shading_table_alloc(1, 1);
3051 		if (!shading_table)
3052 			return -ENOMEM;
3053 		shading_table->enable = 0;
3054 		goto set_lsc;
3055 	}
3056 
3057 	/* Setting a new table. Validate first - all tables must be set */
3058 	for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3059 		if (!st->data[i]) {
3060 			dev_err(asd->isp->dev, "shading table validate failed");
3061 			return -EINVAL;
3062 		}
3063 	}
3064 
3065 	/* Shading table size per color */
3066 	if (st->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
3067 	    st->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) {
3068 		dev_err(asd->isp->dev, "shading table w/h validate failed!");
3069 		return -EINVAL;
3070 	}
3071 
3072 	shading_table = atomisp_css_shading_table_alloc(st->width, st->height);
3073 	if (!shading_table)
3074 		return -ENOMEM;
3075 
3076 	len_table = st->width * st->height * ATOMISP_SC_TYPE_SIZE;
3077 	for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3078 		if (copy_from_compatible(shading_table->data[i],
3079 					 st->data[i], len_table, from_user)) {
3080 			atomisp_css_shading_table_free(shading_table);
3081 			return -EFAULT;
3082 		}
3083 	}
3084 	shading_table->sensor_width = st->sensor_width;
3085 	shading_table->sensor_height = st->sensor_height;
3086 	shading_table->fraction_bits = st->fraction_bits;
3087 	shading_table->enable = st->enable;
3088 
3089 	/* No need to update shading table if it is the same */
3090 	if (old_table &&
3091 	    old_table->sensor_width == shading_table->sensor_width &&
3092 	    old_table->sensor_height == shading_table->sensor_height &&
3093 	    old_table->width == shading_table->width &&
3094 	    old_table->height == shading_table->height &&
3095 	    old_table->fraction_bits == shading_table->fraction_bits &&
3096 	    old_table->enable == shading_table->enable) {
3097 		bool data_is_same = true;
3098 
3099 		for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3100 			if (memcmp(shading_table->data[i], old_table->data[i],
3101 				   len_table) != 0) {
3102 				data_is_same = false;
3103 				break;
3104 			}
3105 		}
3106 
3107 		if (data_is_same) {
3108 			atomisp_css_shading_table_free(shading_table);
3109 			return 0;
3110 		}
3111 	}
3112 
3113 set_lsc:
3114 	/* set LSC to CSS */
3115 	css_param->shading_table = shading_table;
3116 	css_param->update_flag.shading_table = (struct atomisp_shading_table *)shading_table;
3117 	asd->params.sc_en = shading_table;
3118 
3119 	if (old_table)
3120 		atomisp_css_shading_table_free(old_table);
3121 
3122 	return 0;
3123 }
3124 
3125 int atomisp_css_cp_dvs2_coefs(struct atomisp_sub_device *asd,
3126 			      struct ia_css_dvs2_coefficients *coefs,
3127 			      struct atomisp_css_params *css_param,
3128 			      bool from_user)
3129 {
3130 	struct ia_css_dvs_grid_info *cur =
3131 	    atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
3132 	int dvs_hor_coef_bytes, dvs_ver_coef_bytes;
3133 	struct ia_css_dvs2_coefficients dvs2_coefs;
3134 
3135 	if (!coefs || !cur)
3136 		return 0;
3137 
3138 	if (!from_user && css_param->update_flag.dvs2_coefs)
3139 		return 0;
3140 
3141 	if (!IS_ISP2401) {
3142 		if (sizeof(*cur) != sizeof(coefs->grid) ||
3143 		    memcmp(&coefs->grid, cur, sizeof(coefs->grid))) {
3144 			dev_err(asd->isp->dev, "dvs grid mismatch!\n");
3145 			/* If the grid info in the argument differs from the current
3146 			grid info, we tell the caller to reset the grid size and
3147 			try again. */
3148 			return -EAGAIN;
3149 		}
3150 
3151 		if (!coefs->hor_coefs.odd_real ||
3152 		    !coefs->hor_coefs.odd_imag ||
3153 		    !coefs->hor_coefs.even_real ||
3154 		    !coefs->hor_coefs.even_imag ||
3155 		    !coefs->ver_coefs.odd_real ||
3156 		    !coefs->ver_coefs.odd_imag ||
3157 		    !coefs->ver_coefs.even_real ||
3158 		    !coefs->ver_coefs.even_imag)
3159 			return -EINVAL;
3160 
3161 		if (!css_param->dvs2_coeff) {
3162 			/* DIS coefficients. */
3163 			css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
3164 			if (!css_param->dvs2_coeff)
3165 				return -ENOMEM;
3166 		}
3167 
3168 		dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
3169 		dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
3170 		if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
3171 					coefs->hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
3172 		    copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
3173 					coefs->hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
3174 		    copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
3175 					coefs->hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
3176 		    copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
3177 					coefs->hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
3178 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
3179 					coefs->ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
3180 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
3181 					coefs->ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
3182 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
3183 					coefs->ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
3184 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
3185 					coefs->ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
3186 			ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3187 			css_param->dvs2_coeff = NULL;
3188 			return -EFAULT;
3189 		}
3190 	} else {
3191 		if (copy_from_compatible(&dvs2_coefs, coefs,
3192 					sizeof(struct ia_css_dvs2_coefficients),
3193 					from_user)) {
3194 			dev_err(asd->isp->dev, "copy dvs2 coef failed");
3195 			return -EFAULT;
3196 		}
3197 
3198 		if (sizeof(*cur) != sizeof(dvs2_coefs.grid) ||
3199 		    memcmp(&dvs2_coefs.grid, cur, sizeof(dvs2_coefs.grid))) {
3200 			dev_err(asd->isp->dev, "dvs grid mismatch!\n");
3201 			/* If the grid info in the argument differs from the current
3202 			grid info, we tell the caller to reset the grid size and
3203 			try again. */
3204 			return -EAGAIN;
3205 		}
3206 
3207 		if (!dvs2_coefs.hor_coefs.odd_real ||
3208 		    !dvs2_coefs.hor_coefs.odd_imag ||
3209 		    !dvs2_coefs.hor_coefs.even_real ||
3210 		    !dvs2_coefs.hor_coefs.even_imag ||
3211 		    !dvs2_coefs.ver_coefs.odd_real ||
3212 		    !dvs2_coefs.ver_coefs.odd_imag ||
3213 		    !dvs2_coefs.ver_coefs.even_real ||
3214 		    !dvs2_coefs.ver_coefs.even_imag)
3215 			return -EINVAL;
3216 
3217 		if (!css_param->dvs2_coeff) {
3218 			/* DIS coefficients. */
3219 			css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
3220 			if (!css_param->dvs2_coeff)
3221 				return -ENOMEM;
3222 		}
3223 
3224 		dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
3225 		dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
3226 		if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
3227 					dvs2_coefs.hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
3228 		    copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
3229 					dvs2_coefs.hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
3230 		    copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
3231 					dvs2_coefs.hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
3232 		    copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
3233 					dvs2_coefs.hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
3234 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
3235 					dvs2_coefs.ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
3236 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
3237 					dvs2_coefs.ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
3238 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
3239 					dvs2_coefs.ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
3240 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
3241 					dvs2_coefs.ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
3242 			ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3243 			css_param->dvs2_coeff = NULL;
3244 			return -EFAULT;
3245 		}
3246 	}
3247 
3248 	css_param->update_flag.dvs2_coefs =
3249 	    (struct atomisp_dis_coefficients *)css_param->dvs2_coeff;
3250 	return 0;
3251 }
3252 
3253 int atomisp_cp_dvs_6axis_config(struct atomisp_sub_device *asd,
3254 				struct atomisp_dvs_6axis_config *source_6axis_config,
3255 				struct atomisp_css_params *css_param,
3256 				bool from_user)
3257 {
3258 	struct ia_css_dvs_6axis_config *dvs_6axis_config;
3259 	struct ia_css_dvs_6axis_config *old_6axis_config;
3260 	struct ia_css_stream *stream =
3261 		    asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
3262 	struct ia_css_dvs_grid_info *dvs_grid_info =
3263 	    atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
3264 	int ret = -EFAULT;
3265 
3266 	if (!stream) {
3267 		dev_err(asd->isp->dev, "%s: internal error!", __func__);
3268 		return -EINVAL;
3269 	}
3270 
3271 	if (!source_6axis_config || !dvs_grid_info)
3272 		return 0;
3273 
3274 	if (!dvs_grid_info->enable)
3275 		return 0;
3276 
3277 	if (!from_user && css_param->update_flag.dvs_6axis_config)
3278 		return 0;
3279 
3280 	/* check whether need to reallocate for 6 axis config */
3281 	old_6axis_config = css_param->dvs_6axis;
3282 	dvs_6axis_config = old_6axis_config;
3283 
3284 	if (IS_ISP2401) {
3285 		struct ia_css_dvs_6axis_config t_6axis_config;
3286 
3287 		if (copy_from_compatible(&t_6axis_config, source_6axis_config,
3288 					sizeof(struct atomisp_dvs_6axis_config),
3289 					from_user)) {
3290 			dev_err(asd->isp->dev, "copy morph table failed!");
3291 			return -EFAULT;
3292 		}
3293 
3294 		if (old_6axis_config &&
3295 		    (old_6axis_config->width_y != t_6axis_config.width_y ||
3296 		    old_6axis_config->height_y != t_6axis_config.height_y ||
3297 		    old_6axis_config->width_uv != t_6axis_config.width_uv ||
3298 		    old_6axis_config->height_uv != t_6axis_config.height_uv)) {
3299 			ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3300 			css_param->dvs_6axis = NULL;
3301 
3302 			dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3303 			if (!dvs_6axis_config)
3304 				return -ENOMEM;
3305 		} else if (!dvs_6axis_config) {
3306 			dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3307 			if (!dvs_6axis_config)
3308 				return -ENOMEM;
3309 		}
3310 
3311 		dvs_6axis_config->exp_id = t_6axis_config.exp_id;
3312 
3313 		if (copy_from_compatible(dvs_6axis_config->xcoords_y,
3314 					t_6axis_config.xcoords_y,
3315 					t_6axis_config.width_y *
3316 					t_6axis_config.height_y *
3317 					sizeof(*dvs_6axis_config->xcoords_y),
3318 					from_user))
3319 			goto error;
3320 		if (copy_from_compatible(dvs_6axis_config->ycoords_y,
3321 					t_6axis_config.ycoords_y,
3322 					t_6axis_config.width_y *
3323 					t_6axis_config.height_y *
3324 					sizeof(*dvs_6axis_config->ycoords_y),
3325 					from_user))
3326 			goto error;
3327 		if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
3328 					t_6axis_config.xcoords_uv,
3329 					t_6axis_config.width_uv *
3330 					t_6axis_config.height_uv *
3331 					sizeof(*dvs_6axis_config->xcoords_uv),
3332 					from_user))
3333 			goto error;
3334 		if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
3335 					t_6axis_config.ycoords_uv,
3336 					t_6axis_config.width_uv *
3337 					t_6axis_config.height_uv *
3338 					sizeof(*dvs_6axis_config->ycoords_uv),
3339 					from_user))
3340 			goto error;
3341 	} else {
3342 		if (old_6axis_config &&
3343 		    (old_6axis_config->width_y != source_6axis_config->width_y ||
3344 		    old_6axis_config->height_y != source_6axis_config->height_y ||
3345 		    old_6axis_config->width_uv != source_6axis_config->width_uv ||
3346 		    old_6axis_config->height_uv != source_6axis_config->height_uv)) {
3347 			ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3348 			css_param->dvs_6axis = NULL;
3349 
3350 			dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3351 			if (!dvs_6axis_config)
3352 				return -ENOMEM;
3353 		} else if (!dvs_6axis_config) {
3354 			dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3355 			if (!dvs_6axis_config)
3356 				return -ENOMEM;
3357 		}
3358 
3359 		dvs_6axis_config->exp_id = source_6axis_config->exp_id;
3360 
3361 		if (copy_from_compatible(dvs_6axis_config->xcoords_y,
3362 					source_6axis_config->xcoords_y,
3363 					source_6axis_config->width_y *
3364 					source_6axis_config->height_y *
3365 					sizeof(*source_6axis_config->xcoords_y),
3366 					from_user))
3367 			goto error;
3368 		if (copy_from_compatible(dvs_6axis_config->ycoords_y,
3369 					source_6axis_config->ycoords_y,
3370 					source_6axis_config->width_y *
3371 					source_6axis_config->height_y *
3372 					sizeof(*source_6axis_config->ycoords_y),
3373 					from_user))
3374 			goto error;
3375 		if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
3376 					source_6axis_config->xcoords_uv,
3377 					source_6axis_config->width_uv *
3378 					source_6axis_config->height_uv *
3379 					sizeof(*source_6axis_config->xcoords_uv),
3380 					from_user))
3381 			goto error;
3382 		if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
3383 					source_6axis_config->ycoords_uv,
3384 					source_6axis_config->width_uv *
3385 					source_6axis_config->height_uv *
3386 					sizeof(*source_6axis_config->ycoords_uv),
3387 					from_user))
3388 			goto error;
3389 	}
3390 	css_param->dvs_6axis = dvs_6axis_config;
3391 	css_param->update_flag.dvs_6axis_config =
3392 	    (struct atomisp_dvs_6axis_config *)dvs_6axis_config;
3393 	return 0;
3394 
3395 error:
3396 	if (dvs_6axis_config)
3397 		ia_css_dvs2_6axis_config_free(dvs_6axis_config);
3398 	return ret;
3399 }
3400 
3401 int atomisp_cp_morph_table(struct atomisp_sub_device *asd,
3402 			   struct atomisp_morph_table *source_morph_table,
3403 			   struct atomisp_css_params *css_param,
3404 			   bool from_user)
3405 {
3406 	int ret = -EFAULT;
3407 	unsigned int i;
3408 	struct ia_css_morph_table *morph_table;
3409 	struct ia_css_morph_table *old_morph_table;
3410 
3411 	if (!source_morph_table)
3412 		return 0;
3413 
3414 	if (!from_user && css_param->update_flag.morph_table)
3415 		return 0;
3416 
3417 	old_morph_table = css_param->morph_table;
3418 
3419 	if (IS_ISP2401) {
3420 		struct ia_css_morph_table mtbl;
3421 
3422 		if (copy_from_compatible(&mtbl, source_morph_table,
3423 				sizeof(struct atomisp_morph_table),
3424 				from_user)) {
3425 			dev_err(asd->isp->dev, "copy morph table failed!");
3426 			return -EFAULT;
3427 		}
3428 
3429 		morph_table = atomisp_css_morph_table_allocate(
3430 				mtbl.width,
3431 				mtbl.height);
3432 		if (!morph_table)
3433 			return -ENOMEM;
3434 
3435 		for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
3436 			if (copy_from_compatible(morph_table->coordinates_x[i],
3437 						(__force void *)source_morph_table->coordinates_x[i],
3438 						mtbl.height * mtbl.width *
3439 						sizeof(*morph_table->coordinates_x[i]),
3440 						from_user))
3441 				goto error;
3442 
3443 			if (copy_from_compatible(morph_table->coordinates_y[i],
3444 						(__force void *)source_morph_table->coordinates_y[i],
3445 						mtbl.height * mtbl.width *
3446 						sizeof(*morph_table->coordinates_y[i]),
3447 						from_user))
3448 				goto error;
3449 		}
3450 	} else {
3451 		morph_table = atomisp_css_morph_table_allocate(
3452 				source_morph_table->width,
3453 				source_morph_table->height);
3454 		if (!morph_table)
3455 			return -ENOMEM;
3456 
3457 		for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
3458 			if (copy_from_compatible(morph_table->coordinates_x[i],
3459 						(__force void *)source_morph_table->coordinates_x[i],
3460 						source_morph_table->height * source_morph_table->width *
3461 						sizeof(*source_morph_table->coordinates_x[i]),
3462 						from_user))
3463 				goto error;
3464 
3465 			if (copy_from_compatible(morph_table->coordinates_y[i],
3466 						(__force void *)source_morph_table->coordinates_y[i],
3467 						source_morph_table->height * source_morph_table->width *
3468 						sizeof(*source_morph_table->coordinates_y[i]),
3469 						from_user))
3470 				goto error;
3471 		}
3472 	}
3473 
3474 	css_param->morph_table = morph_table;
3475 	if (old_morph_table)
3476 		atomisp_css_morph_table_free(old_morph_table);
3477 	css_param->update_flag.morph_table =
3478 	    (struct atomisp_morph_table *)morph_table;
3479 	return 0;
3480 
3481 error:
3482 	if (morph_table)
3483 		atomisp_css_morph_table_free(morph_table);
3484 	return ret;
3485 }
3486 
3487 int atomisp_makeup_css_parameters(struct atomisp_sub_device *asd,
3488 				  struct atomisp_parameters *arg,
3489 				  struct atomisp_css_params *css_param)
3490 {
3491 	int ret;
3492 
3493 	ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, false);
3494 	if (ret)
3495 		return ret;
3496 	ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, false);
3497 	if (ret)
3498 		return ret;
3499 	ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, false);
3500 	if (ret)
3501 		return ret;
3502 	ret = atomisp_css_cp_dvs2_coefs(asd,
3503 					(struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
3504 					css_param, false);
3505 	if (ret)
3506 		return ret;
3507 	ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
3508 					  css_param, false);
3509 	return ret;
3510 }
3511 
3512 void atomisp_free_css_parameters(struct atomisp_css_params *css_param)
3513 {
3514 	if (css_param->dvs_6axis) {
3515 		ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3516 		css_param->dvs_6axis = NULL;
3517 	}
3518 	if (css_param->dvs2_coeff) {
3519 		ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3520 		css_param->dvs2_coeff = NULL;
3521 	}
3522 	if (css_param->shading_table) {
3523 		ia_css_shading_table_free(css_param->shading_table);
3524 		css_param->shading_table = NULL;
3525 	}
3526 	if (css_param->morph_table) {
3527 		ia_css_morph_table_free(css_param->morph_table);
3528 		css_param->morph_table = NULL;
3529 	}
3530 }
3531 
3532 static void atomisp_move_frame_to_activeq(struct ia_css_frame *frame,
3533 					  struct atomisp_css_params_with_list *param)
3534 {
3535 	struct atomisp_video_pipe *pipe = vb_to_pipe(&frame->vb.vb2_buf);
3536 	unsigned long irqflags;
3537 
3538 	pipe->frame_params[frame->vb.vb2_buf.index] = param;
3539 	spin_lock_irqsave(&pipe->irq_lock, irqflags);
3540 	list_move_tail(&frame->queue, &pipe->activeq);
3541 	spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
3542 }
3543 
3544 /*
3545  * Check parameter queue list and buffer queue list to find out if matched items
3546  * and then set parameter to CSS and enqueue buffer to CSS.
3547  * Of course, if the buffer in buffer waiting list is not bound to a per-frame
3548  * parameter, it will be enqueued into CSS as long as the per-frame setting
3549  * buffers before it get enqueued.
3550  */
3551 void atomisp_handle_parameter_and_buffer(struct atomisp_video_pipe *pipe)
3552 {
3553 	struct atomisp_sub_device *asd = pipe->asd;
3554 	struct ia_css_frame *frame = NULL, *frame_tmp;
3555 	struct atomisp_css_params_with_list *param = NULL, *param_tmp;
3556 	bool need_to_enqueue_buffer = false;
3557 	int i;
3558 
3559 	if (!asd) {
3560 		dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
3561 			__func__, pipe->vdev.name);
3562 		return;
3563 	}
3564 
3565 	lockdep_assert_held(&asd->isp->mutex);
3566 
3567 	if (atomisp_is_vf_pipe(pipe))
3568 		return;
3569 
3570 	/*
3571 	 * CSS/FW requires set parameter and enqueue buffer happen after ISP
3572 	 * is streamon.
3573 	 */
3574 	if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
3575 		return;
3576 
3577 	if (list_empty(&pipe->per_frame_params) ||
3578 	    list_empty(&pipe->buffers_waiting_for_param))
3579 		return;
3580 
3581 	list_for_each_entry_safe(frame, frame_tmp,
3582 				 &pipe->buffers_waiting_for_param, queue) {
3583 		i = frame->vb.vb2_buf.index;
3584 		if (pipe->frame_request_config_id[i]) {
3585 			list_for_each_entry_safe(param, param_tmp,
3586 						 &pipe->per_frame_params, list) {
3587 				if (pipe->frame_request_config_id[i] != param->params.isp_config_id)
3588 					continue;
3589 
3590 				list_del(&param->list);
3591 
3592 				/*
3593 				 * clear the request config id as the buffer
3594 				 * will be handled and enqueued into CSS soon
3595 				 */
3596 				pipe->frame_request_config_id[i] = 0;
3597 				atomisp_move_frame_to_activeq(frame, param);
3598 				need_to_enqueue_buffer = true;
3599 				break;
3600 			}
3601 
3602 			/* If this is the end, stop further loop */
3603 			if (list_entry_is_head(param, &pipe->per_frame_params, list))
3604 				break;
3605 		} else {
3606 			atomisp_move_frame_to_activeq(frame, NULL);
3607 			need_to_enqueue_buffer = true;
3608 		}
3609 	}
3610 
3611 	if (!need_to_enqueue_buffer)
3612 		return;
3613 
3614 	atomisp_qbuffers_to_css(asd);
3615 }
3616 
3617 /*
3618 * Function to configure ISP parameters
3619 */
3620 int atomisp_set_parameters(struct video_device *vdev,
3621 			   struct atomisp_parameters *arg)
3622 {
3623 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
3624 	struct atomisp_sub_device *asd = pipe->asd;
3625 	struct atomisp_css_params_with_list *param = NULL;
3626 	struct atomisp_css_params *css_param = &asd->params.css_param;
3627 	int ret;
3628 
3629 	if (!asd) {
3630 		dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
3631 			__func__, vdev->name);
3632 		return -EINVAL;
3633 	}
3634 
3635 	lockdep_assert_held(&asd->isp->mutex);
3636 
3637 	if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream) {
3638 		dev_err(asd->isp->dev, "%s: internal error!\n", __func__);
3639 		return -EINVAL;
3640 	}
3641 
3642 	dev_dbg(asd->isp->dev,
3643 		"%s: set parameter(per_frame_setting %d) for asd%d with isp_config_id %d of %s\n",
3644 		__func__, arg->per_frame_setting, asd->index,
3645 		arg->isp_config_id, vdev->name);
3646 
3647 	if (IS_ISP2401) {
3648 		if (atomisp_is_vf_pipe(pipe) && arg->per_frame_setting) {
3649 			dev_err(asd->isp->dev, "%s: vf pipe not support per_frame_setting",
3650 				__func__);
3651 			return -EINVAL;
3652 		}
3653 	}
3654 
3655 	if (arg->per_frame_setting && !atomisp_is_vf_pipe(pipe)) {
3656 		/*
3657 		 * Per-frame setting enabled, we allocate a new parameter
3658 		 * buffer to cache the parameters and only when frame buffers
3659 		 * are ready, the parameters will be set to CSS.
3660 		 * per-frame setting only works for the main output frame.
3661 		 */
3662 		param = kvzalloc(sizeof(*param), GFP_KERNEL);
3663 		if (!param) {
3664 			dev_err(asd->isp->dev, "%s: failed to alloc params buffer\n",
3665 				__func__);
3666 			return -ENOMEM;
3667 		}
3668 		css_param = &param->params;
3669 	}
3670 
3671 	ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, true);
3672 	if (ret)
3673 		goto apply_parameter_failed;
3674 
3675 	ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, true);
3676 	if (ret)
3677 		goto apply_parameter_failed;
3678 
3679 	ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, true);
3680 	if (ret)
3681 		goto apply_parameter_failed;
3682 
3683 	ret = atomisp_css_cp_dvs2_coefs(asd,
3684 					(struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
3685 					css_param, true);
3686 	if (ret)
3687 		goto apply_parameter_failed;
3688 
3689 	ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
3690 					  css_param, true);
3691 	if (ret)
3692 		goto apply_parameter_failed;
3693 
3694 	if (!(arg->per_frame_setting && !atomisp_is_vf_pipe(pipe))) {
3695 		/* indicate to CSS that we have parameters to be updated */
3696 		asd->params.css_update_params_needed = true;
3697 	} else {
3698 		list_add_tail(&param->list, &pipe->per_frame_params);
3699 		atomisp_handle_parameter_and_buffer(pipe);
3700 	}
3701 
3702 	return 0;
3703 
3704 apply_parameter_failed:
3705 	if (css_param)
3706 		atomisp_free_css_parameters(css_param);
3707 	kvfree(param);
3708 
3709 	return ret;
3710 }
3711 
3712 /*
3713  * Function to set/get isp parameters to isp
3714  */
3715 int atomisp_param(struct atomisp_sub_device *asd, int flag,
3716 		  struct atomisp_parm *config)
3717 {
3718 	struct ia_css_pipe_config *vp_cfg =
3719 		    &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
3720 		    pipe_configs[IA_CSS_PIPE_ID_VIDEO];
3721 
3722 	/* Read parameter for 3A binary info */
3723 	if (flag == 0) {
3724 		struct ia_css_dvs_grid_info *dvs_grid_info =
3725 		    atomisp_css_get_dvs_grid_info(
3726 			&asd->params.curr_grid_info);
3727 
3728 		atomisp_curr_user_grid_info(asd, &config->info);
3729 
3730 		/* We always return the resolution and stride even if there is
3731 		 * no valid metadata. This allows the caller to get the
3732 		 * information needed to allocate user-space buffers. */
3733 		config->metadata_config.metadata_height = asd->
3734 			stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
3735 			metadata_info.resolution.height;
3736 		config->metadata_config.metadata_stride = asd->
3737 			stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
3738 			metadata_info.stride;
3739 
3740 		/* update dvs grid info */
3741 		if (dvs_grid_info)
3742 			memcpy(&config->dvs_grid,
3743 			       dvs_grid_info,
3744 			       sizeof(struct ia_css_dvs_grid_info));
3745 
3746 		if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
3747 			config->dvs_envelop.width = 0;
3748 			config->dvs_envelop.height = 0;
3749 			return 0;
3750 		}
3751 
3752 		/* update dvs envelop info */
3753 		if (!asd->continuous_mode->val) {
3754 			config->dvs_envelop.width = vp_cfg->dvs_envelope.width;
3755 			config->dvs_envelop.height =
3756 			    vp_cfg->dvs_envelope.height;
3757 		} else {
3758 			unsigned int dvs_w, dvs_h, dvs_w_max, dvs_h_max;
3759 
3760 			dvs_w = vp_cfg->bayer_ds_out_res.width -
3761 				vp_cfg->output_info[0].res.width;
3762 			dvs_h = vp_cfg->bayer_ds_out_res.height -
3763 				vp_cfg->output_info[0].res.height;
3764 			dvs_w_max = rounddown(
3765 					vp_cfg->output_info[0].res.width / 5,
3766 					ATOM_ISP_STEP_WIDTH);
3767 			dvs_h_max = rounddown(
3768 					vp_cfg->output_info[0].res.height / 5,
3769 					ATOM_ISP_STEP_HEIGHT);
3770 
3771 			config->dvs_envelop.width = min(dvs_w, dvs_w_max);
3772 			config->dvs_envelop.height = min(dvs_h, dvs_h_max);
3773 		}
3774 
3775 		return 0;
3776 	}
3777 
3778 	memcpy(&asd->params.css_param.wb_config, &config->wb_config,
3779 	       sizeof(struct ia_css_wb_config));
3780 	memcpy(&asd->params.css_param.ob_config, &config->ob_config,
3781 	       sizeof(struct ia_css_ob_config));
3782 	memcpy(&asd->params.css_param.dp_config, &config->dp_config,
3783 	       sizeof(struct ia_css_dp_config));
3784 	memcpy(&asd->params.css_param.de_config, &config->de_config,
3785 	       sizeof(struct ia_css_de_config));
3786 	memcpy(&asd->params.css_param.dz_config, &config->dz_config,
3787 	       sizeof(struct ia_css_dz_config));
3788 	memcpy(&asd->params.css_param.ce_config, &config->ce_config,
3789 	       sizeof(struct ia_css_ce_config));
3790 	memcpy(&asd->params.css_param.nr_config, &config->nr_config,
3791 	       sizeof(struct ia_css_nr_config));
3792 	memcpy(&asd->params.css_param.ee_config, &config->ee_config,
3793 	       sizeof(struct ia_css_ee_config));
3794 	memcpy(&asd->params.css_param.tnr_config, &config->tnr_config,
3795 	       sizeof(struct ia_css_tnr_config));
3796 
3797 	if (asd->params.color_effect == V4L2_COLORFX_NEGATIVE) {
3798 		asd->params.css_param.cc_config.matrix[3] = -config->cc_config.matrix[3];
3799 		asd->params.css_param.cc_config.matrix[4] = -config->cc_config.matrix[4];
3800 		asd->params.css_param.cc_config.matrix[5] = -config->cc_config.matrix[5];
3801 		asd->params.css_param.cc_config.matrix[6] = -config->cc_config.matrix[6];
3802 		asd->params.css_param.cc_config.matrix[7] = -config->cc_config.matrix[7];
3803 		asd->params.css_param.cc_config.matrix[8] = -config->cc_config.matrix[8];
3804 	}
3805 
3806 	if (asd->params.color_effect != V4L2_COLORFX_SEPIA &&
3807 	    asd->params.color_effect != V4L2_COLORFX_BW) {
3808 		memcpy(&asd->params.css_param.cc_config, &config->cc_config,
3809 		       sizeof(struct ia_css_cc_config));
3810 		asd->params.config.cc_config = &asd->params.css_param.cc_config;
3811 	}
3812 
3813 	asd->params.config.wb_config = &asd->params.css_param.wb_config;
3814 	asd->params.config.ob_config = &asd->params.css_param.ob_config;
3815 	asd->params.config.de_config = &asd->params.css_param.de_config;
3816 	asd->params.config.dz_config = &asd->params.css_param.dz_config;
3817 	asd->params.config.ce_config = &asd->params.css_param.ce_config;
3818 	asd->params.config.dp_config = &asd->params.css_param.dp_config;
3819 	asd->params.config.nr_config = &asd->params.css_param.nr_config;
3820 	asd->params.config.ee_config = &asd->params.css_param.ee_config;
3821 	asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
3822 	asd->params.css_update_params_needed = true;
3823 
3824 	return 0;
3825 }
3826 
3827 /*
3828  * Function to configure color effect of the image
3829  */
3830 int atomisp_color_effect(struct atomisp_sub_device *asd, int flag,
3831 			 __s32 *effect)
3832 {
3833 	struct ia_css_cc_config *cc_config = NULL;
3834 	struct ia_css_macc_table *macc_table = NULL;
3835 	struct ia_css_ctc_table *ctc_table = NULL;
3836 	int ret = 0;
3837 	struct v4l2_control control;
3838 	struct atomisp_device *isp = asd->isp;
3839 
3840 	if (flag == 0) {
3841 		*effect = asd->params.color_effect;
3842 		return 0;
3843 	}
3844 
3845 	control.id = V4L2_CID_COLORFX;
3846 	control.value = *effect;
3847 	ret =
3848 	    v4l2_s_ctrl(NULL, isp->inputs[asd->input_curr].camera->ctrl_handler,
3849 			&control);
3850 	/*
3851 	 * if set color effect to sensor successfully, return
3852 	 * 0 directly.
3853 	 */
3854 	if (!ret) {
3855 		asd->params.color_effect = (u32)*effect;
3856 		return 0;
3857 	}
3858 
3859 	if (*effect == asd->params.color_effect)
3860 		return 0;
3861 
3862 	/*
3863 	 * isp_subdev->params.macc_en should be set to false.
3864 	 */
3865 	asd->params.macc_en = false;
3866 
3867 	switch (*effect) {
3868 	case V4L2_COLORFX_NONE:
3869 		macc_table = &asd->params.css_param.macc_table;
3870 		asd->params.macc_en = true;
3871 		break;
3872 	case V4L2_COLORFX_SEPIA:
3873 		cc_config = &sepia_cc_config;
3874 		break;
3875 	case V4L2_COLORFX_NEGATIVE:
3876 		cc_config = &nega_cc_config;
3877 		break;
3878 	case V4L2_COLORFX_BW:
3879 		cc_config = &mono_cc_config;
3880 		break;
3881 	case V4L2_COLORFX_SKY_BLUE:
3882 		macc_table = &blue_macc_table;
3883 		asd->params.macc_en = true;
3884 		break;
3885 	case V4L2_COLORFX_GRASS_GREEN:
3886 		macc_table = &green_macc_table;
3887 		asd->params.macc_en = true;
3888 		break;
3889 	case V4L2_COLORFX_SKIN_WHITEN_LOW:
3890 		macc_table = &skin_low_macc_table;
3891 		asd->params.macc_en = true;
3892 		break;
3893 	case V4L2_COLORFX_SKIN_WHITEN:
3894 		macc_table = &skin_medium_macc_table;
3895 		asd->params.macc_en = true;
3896 		break;
3897 	case V4L2_COLORFX_SKIN_WHITEN_HIGH:
3898 		macc_table = &skin_high_macc_table;
3899 		asd->params.macc_en = true;
3900 		break;
3901 	case V4L2_COLORFX_VIVID:
3902 		ctc_table = &vivid_ctc_table;
3903 		break;
3904 	default:
3905 		return -EINVAL;
3906 	}
3907 	atomisp_update_capture_mode(asd);
3908 
3909 	if (cc_config)
3910 		asd->params.config.cc_config = cc_config;
3911 	if (macc_table)
3912 		asd->params.config.macc_table = macc_table;
3913 	if (ctc_table)
3914 		atomisp_css_set_ctc_table(asd, ctc_table);
3915 	asd->params.color_effect = (u32)*effect;
3916 	asd->params.css_update_params_needed = true;
3917 	return 0;
3918 }
3919 
3920 /*
3921  * Function to configure bad pixel correction
3922  */
3923 int atomisp_bad_pixel(struct atomisp_sub_device *asd, int flag,
3924 		      __s32 *value)
3925 {
3926 	if (flag == 0) {
3927 		*value = asd->params.bad_pixel_en;
3928 		return 0;
3929 	}
3930 	asd->params.bad_pixel_en = !!*value;
3931 
3932 	return 0;
3933 }
3934 
3935 /*
3936  * Function to configure bad pixel correction params
3937  */
3938 int atomisp_bad_pixel_param(struct atomisp_sub_device *asd, int flag,
3939 			    struct atomisp_dp_config *config)
3940 {
3941 	if (flag == 0) {
3942 		/* Get bad pixel from current setup */
3943 		if (atomisp_css_get_dp_config(asd, config))
3944 			return -EINVAL;
3945 	} else {
3946 		/* Set bad pixel to isp parameters */
3947 		memcpy(&asd->params.css_param.dp_config, config,
3948 		       sizeof(asd->params.css_param.dp_config));
3949 		asd->params.config.dp_config = &asd->params.css_param.dp_config;
3950 		asd->params.css_update_params_needed = true;
3951 	}
3952 
3953 	return 0;
3954 }
3955 
3956 /*
3957  * Function to enable/disable video image stablization
3958  */
3959 int atomisp_video_stable(struct atomisp_sub_device *asd, int flag,
3960 			 __s32 *value)
3961 {
3962 	if (flag == 0)
3963 		*value = asd->params.video_dis_en;
3964 	else
3965 		asd->params.video_dis_en = !!*value;
3966 
3967 	return 0;
3968 }
3969 
3970 /*
3971  * Function to configure fixed pattern noise
3972  */
3973 int atomisp_fixed_pattern(struct atomisp_sub_device *asd, int flag,
3974 			  __s32 *value)
3975 {
3976 	if (flag == 0) {
3977 		*value = asd->params.fpn_en;
3978 		return 0;
3979 	}
3980 
3981 	if (*value == 0) {
3982 		asd->params.fpn_en = false;
3983 		return 0;
3984 	}
3985 
3986 	/* Add function to get black from from sensor with shutter off */
3987 	return 0;
3988 }
3989 
3990 static unsigned int
3991 atomisp_bytesperline_to_padded_width(unsigned int bytesperline,
3992 				     enum ia_css_frame_format format)
3993 {
3994 	switch (format) {
3995 	case IA_CSS_FRAME_FORMAT_UYVY:
3996 	case IA_CSS_FRAME_FORMAT_YUYV:
3997 	case IA_CSS_FRAME_FORMAT_RAW:
3998 	case IA_CSS_FRAME_FORMAT_RGB565:
3999 		return bytesperline / 2;
4000 	case IA_CSS_FRAME_FORMAT_RGBA888:
4001 		return bytesperline / 4;
4002 	/* The following cases could be removed, but we leave them
4003 	   in to document the formats that are included. */
4004 	case IA_CSS_FRAME_FORMAT_NV11:
4005 	case IA_CSS_FRAME_FORMAT_NV12:
4006 	case IA_CSS_FRAME_FORMAT_NV16:
4007 	case IA_CSS_FRAME_FORMAT_NV21:
4008 	case IA_CSS_FRAME_FORMAT_NV61:
4009 	case IA_CSS_FRAME_FORMAT_YV12:
4010 	case IA_CSS_FRAME_FORMAT_YV16:
4011 	case IA_CSS_FRAME_FORMAT_YUV420:
4012 	case IA_CSS_FRAME_FORMAT_YUV420_16:
4013 	case IA_CSS_FRAME_FORMAT_YUV422:
4014 	case IA_CSS_FRAME_FORMAT_YUV422_16:
4015 	case IA_CSS_FRAME_FORMAT_YUV444:
4016 	case IA_CSS_FRAME_FORMAT_YUV_LINE:
4017 	case IA_CSS_FRAME_FORMAT_PLANAR_RGB888:
4018 	case IA_CSS_FRAME_FORMAT_QPLANE6:
4019 	case IA_CSS_FRAME_FORMAT_BINARY_8:
4020 	default:
4021 		return bytesperline;
4022 	}
4023 }
4024 
4025 static int
4026 atomisp_v4l2_framebuffer_to_css_frame(const struct v4l2_framebuffer *arg,
4027 				      struct ia_css_frame **result)
4028 {
4029 	struct ia_css_frame *res = NULL;
4030 	unsigned int padded_width;
4031 	enum ia_css_frame_format sh_format;
4032 	char *tmp_buf = NULL;
4033 	int ret = 0;
4034 
4035 	sh_format = v4l2_fmt_to_sh_fmt(arg->fmt.pixelformat);
4036 	padded_width = atomisp_bytesperline_to_padded_width(
4037 			   arg->fmt.bytesperline, sh_format);
4038 
4039 	/* Note: the padded width on an ia_css_frame is in elements, not in
4040 	   bytes. The RAW frame we use here should always be a 16bit RAW
4041 	   frame. This is why we bytesperline/2 is equal to the padded with */
4042 	if (ia_css_frame_allocate(&res, arg->fmt.width, arg->fmt.height,
4043 				       sh_format, padded_width, 0)) {
4044 		ret = -ENOMEM;
4045 		goto err;
4046 	}
4047 
4048 	tmp_buf = vmalloc(arg->fmt.sizeimage);
4049 	if (!tmp_buf) {
4050 		ret = -ENOMEM;
4051 		goto err;
4052 	}
4053 	if (copy_from_user(tmp_buf, (void __user __force *)arg->base,
4054 			   arg->fmt.sizeimage)) {
4055 		ret = -EFAULT;
4056 		goto err;
4057 	}
4058 
4059 	if (hmm_store(res->data, tmp_buf, arg->fmt.sizeimage)) {
4060 		ret = -EINVAL;
4061 		goto err;
4062 	}
4063 
4064 err:
4065 	if (ret && res)
4066 		ia_css_frame_free(res);
4067 	vfree(tmp_buf);
4068 	if (ret == 0)
4069 		*result = res;
4070 	return ret;
4071 }
4072 
4073 /*
4074  * Function to configure fixed pattern noise table
4075  */
4076 int atomisp_fixed_pattern_table(struct atomisp_sub_device *asd,
4077 				struct v4l2_framebuffer *arg)
4078 {
4079 	struct ia_css_frame *raw_black_frame = NULL;
4080 	int ret;
4081 
4082 	if (!arg)
4083 		return -EINVAL;
4084 
4085 	ret = atomisp_v4l2_framebuffer_to_css_frame(arg, &raw_black_frame);
4086 	if (ret)
4087 		return ret;
4088 
4089 	if (sh_css_set_black_frame(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream,
4090 				   raw_black_frame) != 0)
4091 		return -ENOMEM;
4092 
4093 	ia_css_frame_free(raw_black_frame);
4094 	return ret;
4095 }
4096 
4097 /*
4098  * Function to configure false color correction
4099  */
4100 int atomisp_false_color(struct atomisp_sub_device *asd, int flag,
4101 			__s32 *value)
4102 {
4103 	/* Get nr config from current setup */
4104 	if (flag == 0) {
4105 		*value = asd->params.false_color;
4106 		return 0;
4107 	}
4108 
4109 	/* Set nr config to isp parameters */
4110 	if (*value) {
4111 		asd->params.config.de_config = NULL;
4112 	} else {
4113 		asd->params.css_param.de_config.pixelnoise = 0;
4114 		asd->params.config.de_config = &asd->params.css_param.de_config;
4115 	}
4116 	asd->params.css_update_params_needed = true;
4117 	asd->params.false_color = *value;
4118 	return 0;
4119 }
4120 
4121 /*
4122  * Function to configure bad pixel correction params
4123  */
4124 int atomisp_false_color_param(struct atomisp_sub_device *asd, int flag,
4125 			      struct atomisp_de_config *config)
4126 {
4127 	if (flag == 0) {
4128 		/* Get false color from current setup */
4129 		if (atomisp_css_get_de_config(asd, config))
4130 			return -EINVAL;
4131 	} else {
4132 		/* Set false color to isp parameters */
4133 		memcpy(&asd->params.css_param.de_config, config,
4134 		       sizeof(asd->params.css_param.de_config));
4135 		asd->params.config.de_config = &asd->params.css_param.de_config;
4136 		asd->params.css_update_params_needed = true;
4137 	}
4138 
4139 	return 0;
4140 }
4141 
4142 /*
4143  * Function to configure white balance params
4144  */
4145 int atomisp_white_balance_param(struct atomisp_sub_device *asd, int flag,
4146 				struct atomisp_wb_config *config)
4147 {
4148 	if (flag == 0) {
4149 		/* Get white balance from current setup */
4150 		if (atomisp_css_get_wb_config(asd, config))
4151 			return -EINVAL;
4152 	} else {
4153 		/* Set white balance to isp parameters */
4154 		memcpy(&asd->params.css_param.wb_config, config,
4155 		       sizeof(asd->params.css_param.wb_config));
4156 		asd->params.config.wb_config = &asd->params.css_param.wb_config;
4157 		asd->params.css_update_params_needed = true;
4158 	}
4159 
4160 	return 0;
4161 }
4162 
4163 int atomisp_3a_config_param(struct atomisp_sub_device *asd, int flag,
4164 			    struct atomisp_3a_config *config)
4165 {
4166 	struct atomisp_device *isp = asd->isp;
4167 
4168 	dev_dbg(isp->dev, ">%s %d\n", __func__, flag);
4169 
4170 	if (flag == 0) {
4171 		/* Get white balance from current setup */
4172 		if (atomisp_css_get_3a_config(asd, config))
4173 			return -EINVAL;
4174 	} else {
4175 		/* Set white balance to isp parameters */
4176 		memcpy(&asd->params.css_param.s3a_config, config,
4177 		       sizeof(asd->params.css_param.s3a_config));
4178 		asd->params.config.s3a_config = &asd->params.css_param.s3a_config;
4179 		asd->params.css_update_params_needed = true;
4180 	}
4181 
4182 	dev_dbg(isp->dev, "<%s %d\n", __func__, flag);
4183 	return 0;
4184 }
4185 
4186 /*
4187  * Function to setup digital zoom
4188  */
4189 int atomisp_digital_zoom(struct atomisp_sub_device *asd, int flag,
4190 			 __s32 *value)
4191 {
4192 	u32 zoom;
4193 	struct atomisp_device *isp = asd->isp;
4194 
4195 	unsigned int max_zoom = MRFLD_MAX_ZOOM_FACTOR;
4196 
4197 	if (flag == 0) {
4198 		atomisp_css_get_zoom_factor(asd, &zoom);
4199 		*value = max_zoom - zoom;
4200 	} else {
4201 		if (*value < 0)
4202 			return -EINVAL;
4203 
4204 		zoom = max_zoom - min_t(u32, max_zoom - 1, *value);
4205 		atomisp_css_set_zoom_factor(asd, zoom);
4206 
4207 		dev_dbg(isp->dev, "%s, zoom: %d\n", __func__, zoom);
4208 		asd->params.css_update_params_needed = true;
4209 	}
4210 
4211 	return 0;
4212 }
4213 
4214 /*
4215  * Function to get sensor specific info for current resolution,
4216  * which will be used for auto exposure conversion.
4217  */
4218 int atomisp_get_sensor_mode_data(struct atomisp_sub_device *asd,
4219 				 struct atomisp_sensor_mode_data *config)
4220 {
4221 	struct camera_mipi_info *mipi_info;
4222 	struct atomisp_device *isp = asd->isp;
4223 
4224 	mipi_info = atomisp_to_sensor_mipi_info(
4225 			isp->inputs[asd->input_curr].camera);
4226 	if (!mipi_info)
4227 		return -EINVAL;
4228 
4229 	memcpy(config, &mipi_info->data, sizeof(*config));
4230 	return 0;
4231 }
4232 
4233 static void __atomisp_update_stream_env(struct atomisp_sub_device *asd,
4234 					u16 stream_index, struct atomisp_input_stream_info *stream_info)
4235 {
4236 	int i;
4237 
4238 	/* assign virtual channel id return from sensor driver query */
4239 	asd->stream_env[stream_index].ch_id = stream_info->ch_id;
4240 	asd->stream_env[stream_index].isys_configs = stream_info->isys_configs;
4241 	for (i = 0; i < stream_info->isys_configs; i++) {
4242 		asd->stream_env[stream_index].isys_info[i].input_format =
4243 		    stream_info->isys_info[i].input_format;
4244 		asd->stream_env[stream_index].isys_info[i].width =
4245 		    stream_info->isys_info[i].width;
4246 		asd->stream_env[stream_index].isys_info[i].height =
4247 		    stream_info->isys_info[i].height;
4248 	}
4249 }
4250 
4251 static void __atomisp_init_stream_info(u16 stream_index,
4252 				       struct atomisp_input_stream_info *stream_info)
4253 {
4254 	int i;
4255 
4256 	stream_info->enable = 1;
4257 	stream_info->stream = stream_index;
4258 	stream_info->ch_id = 0;
4259 	stream_info->isys_configs = 0;
4260 	for (i = 0; i < MAX_STREAMS_PER_CHANNEL; i++) {
4261 		stream_info->isys_info[i].input_format = 0;
4262 		stream_info->isys_info[i].width = 0;
4263 		stream_info->isys_info[i].height = 0;
4264 	}
4265 }
4266 
4267 /* This function looks up the closest available resolution. */
4268 int atomisp_try_fmt(struct video_device *vdev, struct v4l2_pix_format *f,
4269 		    bool *res_overflow)
4270 {
4271 	struct atomisp_device *isp = video_get_drvdata(vdev);
4272 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
4273 	struct v4l2_subdev_pad_config pad_cfg;
4274 	struct v4l2_subdev_state pad_state = {
4275 		.pads = &pad_cfg
4276 		};
4277 	struct v4l2_subdev_format format = {
4278 		.which = V4L2_SUBDEV_FORMAT_TRY,
4279 	};
4280 
4281 	struct v4l2_mbus_framefmt *snr_mbus_fmt = &format.format;
4282 	const struct atomisp_format_bridge *fmt;
4283 	struct atomisp_input_stream_info *stream_info =
4284 	    (struct atomisp_input_stream_info *)snr_mbus_fmt->reserved;
4285 	int ret;
4286 
4287 	if (!asd) {
4288 		dev_err(isp->dev, "%s(): asd is NULL, device is %s\n",
4289 			__func__, vdev->name);
4290 		return -EINVAL;
4291 	}
4292 
4293 	if (!isp->inputs[asd->input_curr].camera)
4294 		return -EINVAL;
4295 
4296 	fmt = atomisp_get_format_bridge(f->pixelformat);
4297 	if (!fmt) {
4298 		dev_err(isp->dev, "unsupported pixelformat!\n");
4299 		fmt = atomisp_output_fmts;
4300 	}
4301 
4302 	if (f->width <= 0 || f->height <= 0)
4303 		return -EINVAL;
4304 
4305 	snr_mbus_fmt->code = fmt->mbus_code;
4306 	snr_mbus_fmt->width = f->width;
4307 	snr_mbus_fmt->height = f->height;
4308 
4309 	__atomisp_init_stream_info(ATOMISP_INPUT_STREAM_GENERAL, stream_info);
4310 
4311 	dev_dbg(isp->dev, "try_mbus_fmt: asking for %ux%u\n",
4312 		snr_mbus_fmt->width, snr_mbus_fmt->height);
4313 
4314 	ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
4315 			       pad, set_fmt, &pad_state, &format);
4316 	if (ret)
4317 		return ret;
4318 
4319 	dev_dbg(isp->dev, "try_mbus_fmt: got %ux%u\n",
4320 		snr_mbus_fmt->width, snr_mbus_fmt->height);
4321 
4322 	fmt = atomisp_get_format_bridge_from_mbus(snr_mbus_fmt->code);
4323 	if (!fmt) {
4324 		dev_err(isp->dev, "unknown sensor format 0x%8.8x\n",
4325 			snr_mbus_fmt->code);
4326 		return -EINVAL;
4327 	}
4328 
4329 	f->pixelformat = fmt->pixelformat;
4330 
4331 	/*
4332 	 * If the format is jpeg or custom RAW, then the width and height will
4333 	 * not satisfy the normal atomisp requirements and no need to check
4334 	 * the below conditions. So just assign to what is being returned from
4335 	 * the sensor driver.
4336 	 */
4337 	if (f->pixelformat == V4L2_PIX_FMT_JPEG ||
4338 	    f->pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW) {
4339 		f->width = snr_mbus_fmt->width;
4340 		f->height = snr_mbus_fmt->height;
4341 		return 0;
4342 	}
4343 
4344 	if (!res_overflow || (snr_mbus_fmt->width < f->width &&
4345 			      snr_mbus_fmt->height < f->height)) {
4346 		f->width = snr_mbus_fmt->width;
4347 		f->height = snr_mbus_fmt->height;
4348 		/* Set the flag when resolution requested is
4349 		 * beyond the max value supported by sensor
4350 		 */
4351 		if (res_overflow)
4352 			*res_overflow = true;
4353 	}
4354 
4355 	/* app vs isp */
4356 	f->width = rounddown(clamp_t(u32, f->width, ATOM_ISP_MIN_WIDTH,
4357 				     ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
4358 	f->height = rounddown(clamp_t(u32, f->height, ATOM_ISP_MIN_HEIGHT,
4359 				      ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
4360 
4361 	return 0;
4362 }
4363 
4364 enum mipi_port_id __get_mipi_port(struct atomisp_device *isp,
4365 				  enum atomisp_camera_port port)
4366 {
4367 	switch (port) {
4368 	case ATOMISP_CAMERA_PORT_PRIMARY:
4369 		return MIPI_PORT0_ID;
4370 	case ATOMISP_CAMERA_PORT_SECONDARY:
4371 		return MIPI_PORT1_ID;
4372 	case ATOMISP_CAMERA_PORT_TERTIARY:
4373 		if (MIPI_PORT1_ID + 1 != N_MIPI_PORT_ID)
4374 			return MIPI_PORT1_ID + 1;
4375 		fallthrough;
4376 	default:
4377 		dev_err(isp->dev, "unsupported port: %d\n", port);
4378 		return MIPI_PORT0_ID;
4379 	}
4380 }
4381 
4382 static inline int atomisp_set_sensor_mipi_to_isp(
4383     struct atomisp_sub_device *asd,
4384     enum atomisp_input_stream_id stream_id,
4385     struct camera_mipi_info *mipi_info)
4386 {
4387 	struct v4l2_control ctrl;
4388 	struct atomisp_device *isp = asd->isp;
4389 	const struct atomisp_in_fmt_conv *fc;
4390 	int mipi_freq = 0;
4391 	unsigned int input_format, bayer_order;
4392 
4393 	ctrl.id = V4L2_CID_LINK_FREQ;
4394 	if (v4l2_g_ctrl
4395 	    (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl) == 0)
4396 		mipi_freq = ctrl.value;
4397 
4398 	if (asd->stream_env[stream_id].isys_configs == 1) {
4399 		input_format =
4400 		    asd->stream_env[stream_id].isys_info[0].input_format;
4401 		atomisp_css_isys_set_format(asd, stream_id,
4402 					    input_format, IA_CSS_STREAM_DEFAULT_ISYS_STREAM_IDX);
4403 	} else if (asd->stream_env[stream_id].isys_configs == 2) {
4404 		atomisp_css_isys_two_stream_cfg_update_stream1(
4405 		    asd, stream_id,
4406 		    asd->stream_env[stream_id].isys_info[0].input_format,
4407 		    asd->stream_env[stream_id].isys_info[0].width,
4408 		    asd->stream_env[stream_id].isys_info[0].height);
4409 
4410 		atomisp_css_isys_two_stream_cfg_update_stream2(
4411 		    asd, stream_id,
4412 		    asd->stream_env[stream_id].isys_info[1].input_format,
4413 		    asd->stream_env[stream_id].isys_info[1].width,
4414 		    asd->stream_env[stream_id].isys_info[1].height);
4415 	}
4416 
4417 	/* Compatibility for sensors which provide no media bus code
4418 	 * in s_mbus_framefmt() nor support pad formats. */
4419 	if (mipi_info->input_format != -1) {
4420 		bayer_order = mipi_info->raw_bayer_order;
4421 
4422 		/* Input stream config is still needs configured */
4423 		/* TODO: Check if this is necessary */
4424 		fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
4425 			 mipi_info->input_format);
4426 		if (!fc)
4427 			return -EINVAL;
4428 		input_format = fc->atomisp_in_fmt;
4429 	} else {
4430 		struct v4l2_mbus_framefmt *sink;
4431 
4432 		sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4433 					       V4L2_SUBDEV_FORMAT_ACTIVE,
4434 					       ATOMISP_SUBDEV_PAD_SINK);
4435 		fc = atomisp_find_in_fmt_conv(sink->code);
4436 		if (!fc)
4437 			return -EINVAL;
4438 		input_format = fc->atomisp_in_fmt;
4439 		bayer_order = fc->bayer_order;
4440 	}
4441 
4442 	atomisp_css_input_set_format(asd, stream_id, input_format);
4443 	atomisp_css_input_set_bayer_order(asd, stream_id, bayer_order);
4444 
4445 	fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
4446 		 mipi_info->metadata_format);
4447 	if (!fc)
4448 		return -EINVAL;
4449 	input_format = fc->atomisp_in_fmt;
4450 	atomisp_css_input_configure_port(asd,
4451 					 __get_mipi_port(asd->isp, mipi_info->port),
4452 					 mipi_info->num_lanes,
4453 					 0xffff4, mipi_freq,
4454 					 input_format,
4455 					 mipi_info->metadata_width,
4456 					 mipi_info->metadata_height);
4457 	return 0;
4458 }
4459 
4460 static int __enable_continuous_mode(struct atomisp_sub_device *asd,
4461 				    bool enable)
4462 {
4463 	struct atomisp_device *isp = asd->isp;
4464 
4465 	dev_dbg(isp->dev,
4466 		"continuous mode %d, raw buffers %d, stop preview %d\n",
4467 		enable, asd->continuous_raw_buffer_size->val,
4468 		!asd->continuous_viewfinder->val);
4469 
4470 	if (!IS_ISP2401)
4471 		atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_PRIMARY);
4472 	else
4473 		atomisp_update_capture_mode(asd);
4474 
4475 	/* in case of ANR, force capture pipe to offline mode */
4476 	atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
4477 					  asd->params.low_light ? false : !enable);
4478 	atomisp_css_preview_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
4479 					  !enable);
4480 	atomisp_css_enable_continuous(asd, enable);
4481 	atomisp_css_enable_cvf(asd, asd->continuous_viewfinder->val);
4482 
4483 	atomisp_css_continuous_set_num_raw_frames(asd,
4484 		asd->continuous_raw_buffer_size->val);
4485 
4486 	if (!enable) {
4487 		atomisp_css_enable_raw_binning(asd, false);
4488 		atomisp_css_input_set_two_pixels_per_clock(asd, false);
4489 	}
4490 
4491 	if (isp->inputs[asd->input_curr].type != FILE_INPUT)
4492 		atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
4493 
4494 	return atomisp_update_run_mode(asd);
4495 }
4496 
4497 static int configure_pp_input_nop(struct atomisp_sub_device *asd,
4498 				  unsigned int width, unsigned int height)
4499 {
4500 	return 0;
4501 }
4502 
4503 static int configure_output_nop(struct atomisp_sub_device *asd,
4504 				unsigned int width, unsigned int height,
4505 				unsigned int min_width,
4506 				enum ia_css_frame_format sh_fmt)
4507 {
4508 	return 0;
4509 }
4510 
4511 static int get_frame_info_nop(struct atomisp_sub_device *asd,
4512 			      struct ia_css_frame_info *finfo)
4513 {
4514 	return 0;
4515 }
4516 
4517 /*
4518  * Resets CSS parameters that depend on input resolution.
4519  *
4520  * Update params like CSS RAW binning, 2ppc mode and pp_input
4521  * which depend on input size, but are not automatically
4522  * handled in CSS when the input resolution is changed.
4523  */
4524 static int css_input_resolution_changed(struct atomisp_sub_device *asd,
4525 					struct v4l2_mbus_framefmt *ffmt)
4526 {
4527 	struct atomisp_metadata_buf *md_buf = NULL, *_md_buf;
4528 	unsigned int i;
4529 
4530 	dev_dbg(asd->isp->dev, "css_input_resolution_changed to %ux%u\n",
4531 		ffmt->width, ffmt->height);
4532 
4533 	if (IS_ISP2401)
4534 		atomisp_css_input_set_two_pixels_per_clock(asd, false);
4535 	else
4536 		atomisp_css_input_set_two_pixels_per_clock(asd, true);
4537 
4538 	if (asd->continuous_mode->val) {
4539 		/* Note for all checks: ffmt includes pad_w+pad_h */
4540 		if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO ||
4541 		    (ffmt->width >= 2048 || ffmt->height >= 1536)) {
4542 			/*
4543 			 * For preview pipe, enable only if resolution
4544 			 * is >= 3M for ISP2400.
4545 			 */
4546 			atomisp_css_enable_raw_binning(asd, true);
4547 		}
4548 	}
4549 	/*
4550 	 * If sensor input changed, which means metadata resolution changed
4551 	 * together. Release all metadata buffers here to let it re-allocated
4552 	 * next time in reqbufs.
4553 	 */
4554 	for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
4555 		list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
4556 					 list) {
4557 			atomisp_css_free_metadata_buffer(md_buf);
4558 			list_del(&md_buf->list);
4559 			kfree(md_buf);
4560 		}
4561 	}
4562 	return 0;
4563 
4564 	/*
4565 	 * TODO: atomisp_css_preview_configure_pp_input() not
4566 	 *       reset due to CSS bug tracked as PSI BZ 115124
4567 	 */
4568 }
4569 
4570 static int atomisp_set_fmt_to_isp(struct video_device *vdev,
4571 				  struct ia_css_frame_info *output_info,
4572 				  struct ia_css_frame_info *raw_output_info,
4573 				  struct v4l2_pix_format *pix,
4574 				  unsigned int source_pad)
4575 {
4576 	struct camera_mipi_info *mipi_info;
4577 	struct atomisp_device *isp = video_get_drvdata(vdev);
4578 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
4579 	const struct atomisp_format_bridge *format;
4580 	struct v4l2_rect *isp_sink_crop;
4581 	enum ia_css_pipe_id pipe_id;
4582 	struct v4l2_subdev_fh fh;
4583 	int (*configure_output)(struct atomisp_sub_device *asd,
4584 				unsigned int width, unsigned int height,
4585 				unsigned int min_width,
4586 				enum ia_css_frame_format sh_fmt) =
4587 				    configure_output_nop;
4588 	int (*get_frame_info)(struct atomisp_sub_device *asd,
4589 			      struct ia_css_frame_info *finfo) =
4590 				  get_frame_info_nop;
4591 	int (*configure_pp_input)(struct atomisp_sub_device *asd,
4592 				  unsigned int width, unsigned int height) =
4593 				      configure_pp_input_nop;
4594 	const struct atomisp_in_fmt_conv *fc;
4595 	int ret, i;
4596 
4597 	if (!asd) {
4598 		dev_err(isp->dev, "%s(): asd is NULL, device is %s\n",
4599 			__func__, vdev->name);
4600 		return -EINVAL;
4601 	}
4602 
4603 	v4l2_fh_init(&fh.vfh, vdev);
4604 
4605 	isp_sink_crop = atomisp_subdev_get_rect(
4606 			    &asd->subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
4607 			    ATOMISP_SUBDEV_PAD_SINK, V4L2_SEL_TGT_CROP);
4608 
4609 	format = atomisp_get_format_bridge(pix->pixelformat);
4610 	if (!format)
4611 		return -EINVAL;
4612 
4613 	if (isp->inputs[asd->input_curr].type != TEST_PATTERN &&
4614 	    isp->inputs[asd->input_curr].type != FILE_INPUT) {
4615 		mipi_info = atomisp_to_sensor_mipi_info(
4616 				isp->inputs[asd->input_curr].camera);
4617 		if (!mipi_info) {
4618 			dev_err(isp->dev, "mipi_info is NULL\n");
4619 			return -EINVAL;
4620 		}
4621 		if (atomisp_set_sensor_mipi_to_isp(asd, ATOMISP_INPUT_STREAM_GENERAL,
4622 						   mipi_info))
4623 			return -EINVAL;
4624 		fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
4625 			 mipi_info->input_format);
4626 		if (!fc)
4627 			fc = atomisp_find_in_fmt_conv(
4628 				 atomisp_subdev_get_ffmt(&asd->subdev,
4629 							 NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
4630 							 ATOMISP_SUBDEV_PAD_SINK)->code);
4631 		if (!fc)
4632 			return -EINVAL;
4633 		if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW &&
4634 		    raw_output_format_match_input(fc->atomisp_in_fmt,
4635 						  pix->pixelformat))
4636 			return -EINVAL;
4637 	}
4638 
4639 	/*
4640 	 * Configure viewfinder also when vfpp is disabled: the
4641 	 * CSS still requires viewfinder configuration.
4642 	 */
4643 	if (asd->fmt_auto->val ||
4644 	    asd->vfpp->val != ATOMISP_VFPP_ENABLE) {
4645 		struct v4l2_rect vf_size = {0};
4646 		struct v4l2_mbus_framefmt vf_ffmt = {0};
4647 
4648 		if (pix->width < 640 || pix->height < 480) {
4649 			vf_size.width = pix->width;
4650 			vf_size.height = pix->height;
4651 		} else {
4652 			vf_size.width = 640;
4653 			vf_size.height = 480;
4654 		}
4655 
4656 		/* FIXME: proper format name for this one. See
4657 		   atomisp_output_fmts[] in atomisp_v4l2.c */
4658 		vf_ffmt.code = V4L2_MBUS_FMT_CUSTOM_YUV420;
4659 
4660 		atomisp_subdev_set_selection(&asd->subdev, fh.state,
4661 					     V4L2_SUBDEV_FORMAT_ACTIVE,
4662 					     ATOMISP_SUBDEV_PAD_SOURCE_VF,
4663 					     V4L2_SEL_TGT_COMPOSE, 0, &vf_size);
4664 		atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
4665 					V4L2_SUBDEV_FORMAT_ACTIVE,
4666 					ATOMISP_SUBDEV_PAD_SOURCE_VF, &vf_ffmt);
4667 		asd->video_out_vf.sh_fmt = IA_CSS_FRAME_FORMAT_NV12;
4668 
4669 		if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
4670 			atomisp_css_video_configure_viewfinder(asd,
4671 							       vf_size.width, vf_size.height, 0,
4672 							       asd->video_out_vf.sh_fmt);
4673 		} else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4674 			if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
4675 			    source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO)
4676 				atomisp_css_video_configure_viewfinder(asd,
4677 								       vf_size.width, vf_size.height, 0,
4678 								       asd->video_out_vf.sh_fmt);
4679 			else
4680 				atomisp_css_capture_configure_viewfinder(asd,
4681 					vf_size.width, vf_size.height, 0,
4682 					asd->video_out_vf.sh_fmt);
4683 		} else if (source_pad != ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
4684 			   asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
4685 			atomisp_css_capture_configure_viewfinder(asd,
4686 				vf_size.width, vf_size.height, 0,
4687 				asd->video_out_vf.sh_fmt);
4688 		}
4689 	}
4690 
4691 	if (asd->continuous_mode->val) {
4692 		ret = __enable_continuous_mode(asd, true);
4693 		if (ret)
4694 			return -EINVAL;
4695 	}
4696 
4697 	atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
4698 
4699 	for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++)
4700 		asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipe_extra_configs[i].disable_vf_pp = asd->vfpp->val != ATOMISP_VFPP_ENABLE;
4701 
4702 	/* ISP2401 new input system need to use copy pipe */
4703 	if (asd->copy_mode) {
4704 		pipe_id = IA_CSS_PIPE_ID_COPY;
4705 		atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL, false);
4706 	} else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
4707 		/* video same in continuouscapture and online modes */
4708 		configure_output = atomisp_css_video_configure_output;
4709 		get_frame_info = atomisp_css_video_get_output_frame_info;
4710 		pipe_id = IA_CSS_PIPE_ID_VIDEO;
4711 	} else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4712 		if (!asd->continuous_mode->val) {
4713 			configure_output = atomisp_css_video_configure_output;
4714 			get_frame_info =
4715 			    atomisp_css_video_get_output_frame_info;
4716 			pipe_id = IA_CSS_PIPE_ID_VIDEO;
4717 		} else {
4718 			if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
4719 			    source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO) {
4720 				configure_output =
4721 				    atomisp_css_video_configure_output;
4722 				get_frame_info =
4723 				    atomisp_css_video_get_output_frame_info;
4724 				configure_pp_input =
4725 				    atomisp_css_video_configure_pp_input;
4726 				pipe_id = IA_CSS_PIPE_ID_VIDEO;
4727 			} else {
4728 				configure_output =
4729 				    atomisp_css_capture_configure_output;
4730 				get_frame_info =
4731 				    atomisp_css_capture_get_output_frame_info;
4732 				configure_pp_input =
4733 				    atomisp_css_capture_configure_pp_input;
4734 				pipe_id = IA_CSS_PIPE_ID_CAPTURE;
4735 
4736 				atomisp_update_capture_mode(asd);
4737 				atomisp_css_capture_enable_online(asd,
4738 								  ATOMISP_INPUT_STREAM_GENERAL,
4739 								  false);
4740 			}
4741 		}
4742 	} else if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW) {
4743 		configure_output = atomisp_css_preview_configure_output;
4744 		get_frame_info = atomisp_css_preview_get_output_frame_info;
4745 		configure_pp_input = atomisp_css_preview_configure_pp_input;
4746 		pipe_id = IA_CSS_PIPE_ID_PREVIEW;
4747 	} else {
4748 		/* CSS doesn't support low light mode on SOC cameras, so disable
4749 		 * it. FIXME: if this is done elsewhere, it gives corrupted
4750 		 * colors into thumbnail image.
4751 		 */
4752 		if (isp->inputs[asd->input_curr].type == SOC_CAMERA)
4753 			asd->params.low_light = false;
4754 
4755 		if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
4756 			atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
4757 			atomisp_css_enable_dz(asd, false);
4758 		} else {
4759 			atomisp_update_capture_mode(asd);
4760 		}
4761 
4762 		if (!asd->continuous_mode->val)
4763 			/* in case of ANR, force capture pipe to offline mode */
4764 			atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
4765 							  asd->params.low_light ?
4766 							  false : asd->params.online_process);
4767 
4768 		configure_output = atomisp_css_capture_configure_output;
4769 		get_frame_info = atomisp_css_capture_get_output_frame_info;
4770 		configure_pp_input = atomisp_css_capture_configure_pp_input;
4771 		pipe_id = IA_CSS_PIPE_ID_CAPTURE;
4772 
4773 		if (!asd->params.online_process &&
4774 		    !asd->continuous_mode->val) {
4775 			ret = atomisp_css_capture_get_output_raw_frame_info(asd,
4776 				raw_output_info);
4777 			if (ret)
4778 				return ret;
4779 		}
4780 		if (!asd->continuous_mode->val && asd->run_mode->val
4781 		    != ATOMISP_RUN_MODE_STILL_CAPTURE) {
4782 			dev_err(isp->dev,
4783 				"Need to set the running mode first\n");
4784 			asd->run_mode->val = ATOMISP_RUN_MODE_STILL_CAPTURE;
4785 		}
4786 	}
4787 
4788 	/*
4789 	 * to SOC camera, use yuvpp pipe.
4790 	 */
4791 	if (ATOMISP_USE_YUVPP(asd))
4792 		pipe_id = IA_CSS_PIPE_ID_YUVPP;
4793 
4794 	if (asd->copy_mode)
4795 		ret = atomisp_css_copy_configure_output(asd, ATOMISP_INPUT_STREAM_GENERAL,
4796 							pix->width, pix->height,
4797 							format->planar ? pix->bytesperline :
4798 							pix->bytesperline * 8 / format->depth,
4799 							format->sh_fmt);
4800 	else
4801 		ret = configure_output(asd, pix->width, pix->height,
4802 				       format->planar ? pix->bytesperline :
4803 				       pix->bytesperline * 8 / format->depth,
4804 				       format->sh_fmt);
4805 	if (ret) {
4806 		dev_err(isp->dev, "configure_output %ux%u, format %8.8x\n",
4807 			pix->width, pix->height, format->sh_fmt);
4808 		return -EINVAL;
4809 	}
4810 
4811 	ret = configure_pp_input(asd, isp_sink_crop->width, isp_sink_crop->height);
4812 	if (ret) {
4813 		dev_err(isp->dev, "configure_pp_input %ux%u\n",
4814 			isp_sink_crop->width,
4815 			isp_sink_crop->height);
4816 		return -EINVAL;
4817 	}
4818 	if (asd->copy_mode)
4819 		ret = atomisp_css_copy_get_output_frame_info(asd,
4820 							     ATOMISP_INPUT_STREAM_GENERAL,
4821 							     output_info);
4822 	else
4823 		ret = get_frame_info(asd, output_info);
4824 	if (ret) {
4825 		dev_err(isp->dev, "__get_frame_info %ux%u (padded to %u) returned %d\n",
4826 			pix->width, pix->height, pix->bytesperline, ret);
4827 		return ret;
4828 	}
4829 
4830 	atomisp_update_grid_info(asd, pipe_id, source_pad);
4831 
4832 	/* Free the raw_dump buffer first */
4833 	ia_css_frame_free(asd->raw_output_frame);
4834 	asd->raw_output_frame = NULL;
4835 
4836 	if (!asd->continuous_mode->val && !asd->params.online_process &&
4837 	    ia_css_frame_allocate_from_info(&asd->raw_output_frame,
4838 		    raw_output_info))
4839 		return -ENOMEM;
4840 
4841 	return 0;
4842 }
4843 
4844 static void atomisp_get_dis_envelop(struct atomisp_sub_device *asd,
4845 				    unsigned int width, unsigned int height,
4846 				    unsigned int *dvs_env_w, unsigned int *dvs_env_h)
4847 {
4848 	struct atomisp_device *isp = asd->isp;
4849 
4850 	/* if subdev type is SOC camera,we do not need to set DVS */
4851 	if (isp->inputs[asd->input_curr].type == SOC_CAMERA)
4852 		asd->params.video_dis_en = false;
4853 
4854 	if (asd->params.video_dis_en &&
4855 	    asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4856 		/* envelope is 20% of the output resolution */
4857 		/*
4858 		 * dvs envelope cannot be round up.
4859 		 * it would cause ISP timeout and color switch issue
4860 		 */
4861 		*dvs_env_w = rounddown(width / 5, ATOM_ISP_STEP_WIDTH);
4862 		*dvs_env_h = rounddown(height / 5, ATOM_ISP_STEP_HEIGHT);
4863 	}
4864 
4865 	asd->params.dis_proj_data_valid = false;
4866 	asd->params.css_update_params_needed = true;
4867 }
4868 
4869 static void atomisp_check_copy_mode(struct atomisp_sub_device *asd,
4870 				    int source_pad, struct v4l2_pix_format *f)
4871 {
4872 	struct v4l2_mbus_framefmt *sink, *src;
4873 
4874 	if (!IS_ISP2401) {
4875 		/* Only used for the new input system */
4876 		asd->copy_mode = false;
4877 		return;
4878 	}
4879 
4880 	sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4881 				       V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SINK);
4882 	src = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4883 				      V4L2_SUBDEV_FORMAT_ACTIVE, source_pad);
4884 
4885 	if (sink->code == src->code && sink->width == f->width && sink->height == f->height)
4886 		asd->copy_mode = true;
4887 	else
4888 		asd->copy_mode = false;
4889 
4890 	dev_dbg(asd->isp->dev, "copy_mode: %d\n", asd->copy_mode);
4891 }
4892 
4893 static int atomisp_set_fmt_to_snr(struct video_device *vdev,
4894 				  struct v4l2_pix_format *f, unsigned int pixelformat,
4895 				  unsigned int padding_w, unsigned int padding_h,
4896 				  unsigned int dvs_env_w, unsigned int dvs_env_h)
4897 {
4898 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4899 	struct atomisp_sub_device *asd = pipe->asd;
4900 	const struct atomisp_format_bridge *format;
4901 	struct v4l2_subdev_pad_config pad_cfg;
4902 	struct v4l2_subdev_state pad_state = {
4903 		.pads = &pad_cfg
4904 		};
4905 	struct v4l2_subdev_format vformat = {
4906 		.which = V4L2_SUBDEV_FORMAT_TRY,
4907 	};
4908 	struct v4l2_mbus_framefmt *ffmt = &vformat.format;
4909 	struct v4l2_mbus_framefmt *req_ffmt;
4910 	struct atomisp_device *isp;
4911 	struct atomisp_input_stream_info *stream_info =
4912 	    (struct atomisp_input_stream_info *)ffmt->reserved;
4913 	int source_pad = atomisp_subdev_source_pad(vdev);
4914 	struct v4l2_subdev_fh fh;
4915 	int ret;
4916 
4917 	if (!asd) {
4918 		dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
4919 			__func__, vdev->name);
4920 		return -EINVAL;
4921 	}
4922 
4923 	isp = asd->isp;
4924 
4925 	v4l2_fh_init(&fh.vfh, vdev);
4926 
4927 	format = atomisp_get_format_bridge(pixelformat);
4928 	if (!format)
4929 		return -EINVAL;
4930 
4931 	v4l2_fill_mbus_format(ffmt, f, format->mbus_code);
4932 	ffmt->height += padding_h + dvs_env_h;
4933 	ffmt->width += padding_w + dvs_env_w;
4934 
4935 	dev_dbg(isp->dev, "s_mbus_fmt: ask %ux%u (padding %ux%u, dvs %ux%u)\n",
4936 		ffmt->width, ffmt->height, padding_w, padding_h,
4937 		dvs_env_w, dvs_env_h);
4938 
4939 	__atomisp_init_stream_info(ATOMISP_INPUT_STREAM_GENERAL, stream_info);
4940 
4941 	req_ffmt = ffmt;
4942 
4943 	/* Disable dvs if resolution can't be supported by sensor */
4944 	if (asd->params.video_dis_en &&
4945 	    source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO) {
4946 		vformat.which = V4L2_SUBDEV_FORMAT_TRY;
4947 		ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
4948 				       pad, set_fmt, &pad_state, &vformat);
4949 		if (ret)
4950 			return ret;
4951 
4952 		dev_dbg(isp->dev, "video dis: sensor width: %d, height: %d\n",
4953 			ffmt->width, ffmt->height);
4954 
4955 		if (ffmt->width < req_ffmt->width ||
4956 		    ffmt->height < req_ffmt->height) {
4957 			req_ffmt->height -= dvs_env_h;
4958 			req_ffmt->width -= dvs_env_w;
4959 			ffmt = req_ffmt;
4960 			dev_warn(isp->dev,
4961 				 "can not enable video dis due to sensor limitation.");
4962 			asd->params.video_dis_en = false;
4963 		}
4964 	}
4965 	vformat.which = V4L2_SUBDEV_FORMAT_ACTIVE;
4966 	ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera, pad,
4967 			       set_fmt, NULL, &vformat);
4968 	if (ret)
4969 		return ret;
4970 
4971 	__atomisp_update_stream_env(asd, ATOMISP_INPUT_STREAM_GENERAL, stream_info);
4972 
4973 	dev_dbg(isp->dev, "sensor width: %d, height: %d\n",
4974 		ffmt->width, ffmt->height);
4975 
4976 	if (ffmt->width < ATOM_ISP_STEP_WIDTH ||
4977 	    ffmt->height < ATOM_ISP_STEP_HEIGHT)
4978 		return -EINVAL;
4979 
4980 	if (asd->params.video_dis_en &&
4981 	    source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO &&
4982 	    (ffmt->width < req_ffmt->width || ffmt->height < req_ffmt->height)) {
4983 		dev_warn(isp->dev,
4984 			 "can not enable video dis due to sensor limitation.");
4985 		asd->params.video_dis_en = false;
4986 	}
4987 
4988 	atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
4989 				V4L2_SUBDEV_FORMAT_ACTIVE,
4990 				ATOMISP_SUBDEV_PAD_SINK, ffmt);
4991 
4992 	return css_input_resolution_changed(asd, ffmt);
4993 }
4994 
4995 int atomisp_set_fmt(struct video_device *vdev, struct v4l2_format *f)
4996 {
4997 	struct atomisp_device *isp = video_get_drvdata(vdev);
4998 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4999 	struct atomisp_sub_device *asd = pipe->asd;
5000 	const struct atomisp_format_bridge *format_bridge;
5001 	const struct atomisp_format_bridge *snr_format_bridge;
5002 	struct ia_css_frame_info output_info, raw_output_info;
5003 	struct v4l2_pix_format snr_fmt;
5004 	struct v4l2_pix_format backup_fmt, s_fmt;
5005 	unsigned int dvs_env_w = 0, dvs_env_h = 0;
5006 	unsigned int padding_w = pad_w, padding_h = pad_h;
5007 	bool res_overflow = false, crop_needs_override = false;
5008 	struct v4l2_mbus_framefmt *isp_sink_fmt;
5009 	struct v4l2_mbus_framefmt isp_source_fmt = {0};
5010 	struct v4l2_subdev_format vformat = {
5011 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
5012 	};
5013 	struct v4l2_mbus_framefmt *ffmt = &vformat.format;
5014 	struct v4l2_rect isp_sink_crop;
5015 	u16 source_pad = atomisp_subdev_source_pad(vdev);
5016 	struct v4l2_subdev_fh fh;
5017 	int ret;
5018 
5019 	ret = atomisp_pipe_check(pipe, true);
5020 	if (ret)
5021 		return ret;
5022 
5023 	if (source_pad >= ATOMISP_SUBDEV_PADS_NUM)
5024 		return -EINVAL;
5025 
5026 	dev_dbg(isp->dev,
5027 		"setting resolution %ux%u on pad %u for asd%d, bytesperline %u\n",
5028 		f->fmt.pix.width, f->fmt.pix.height, source_pad,
5029 		asd->index, f->fmt.pix.bytesperline);
5030 
5031 	v4l2_fh_init(&fh.vfh, vdev);
5032 
5033 	format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
5034 	if (!format_bridge)
5035 		return -EINVAL;
5036 
5037 	/* Currently, raw formats are broken!!! */
5038 
5039 	if (format_bridge->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
5040 		f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
5041 
5042 		format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
5043 		if (!format_bridge)
5044 			return -EINVAL;
5045 	}
5046 	pipe->sh_fmt = format_bridge->sh_fmt;
5047 	pipe->pix.pixelformat = f->fmt.pix.pixelformat;
5048 
5049 	/* Ensure that the resolution is equal or below the maximum supported */
5050 
5051 	vformat.which = V4L2_SUBDEV_FORMAT_ACTIVE;
5052 	v4l2_fill_mbus_format(ffmt, &f->fmt.pix, format_bridge->mbus_code);
5053 	ffmt->height += padding_h;
5054 	ffmt->width += padding_w;
5055 
5056 	ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera, pad,
5057 			       set_fmt, NULL, &vformat);
5058 	if (ret)
5059 		return ret;
5060 
5061 	f->fmt.pix.width = ffmt->width - padding_w;
5062 	f->fmt.pix.height = ffmt->height - padding_h;
5063 
5064 	snr_fmt = f->fmt.pix;
5065 	backup_fmt = snr_fmt;
5066 
5067 	/**********************************************************************/
5068 
5069 	if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VF ||
5070 	    (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW
5071 	     && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)) {
5072 		if (asd->fmt_auto->val) {
5073 			struct v4l2_rect *capture_comp;
5074 			struct v4l2_rect r = {0};
5075 
5076 			r.width = f->fmt.pix.width;
5077 			r.height = f->fmt.pix.height;
5078 
5079 			if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW)
5080 				capture_comp = atomisp_subdev_get_rect(
5081 						   &asd->subdev, NULL,
5082 						   V4L2_SUBDEV_FORMAT_ACTIVE,
5083 						   ATOMISP_SUBDEV_PAD_SOURCE_VIDEO,
5084 						   V4L2_SEL_TGT_COMPOSE);
5085 			else
5086 				capture_comp = atomisp_subdev_get_rect(
5087 						   &asd->subdev, NULL,
5088 						   V4L2_SUBDEV_FORMAT_ACTIVE,
5089 						   ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE,
5090 						   V4L2_SEL_TGT_COMPOSE);
5091 
5092 			if (capture_comp->width < r.width
5093 			    || capture_comp->height < r.height) {
5094 				r.width = capture_comp->width;
5095 				r.height = capture_comp->height;
5096 			}
5097 
5098 			atomisp_subdev_set_selection(
5099 			    &asd->subdev, fh.state,
5100 			    V4L2_SUBDEV_FORMAT_ACTIVE, source_pad,
5101 			    V4L2_SEL_TGT_COMPOSE, 0, &r);
5102 
5103 			f->fmt.pix.width = r.width;
5104 			f->fmt.pix.height = r.height;
5105 		}
5106 
5107 		if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW) {
5108 			atomisp_css_video_configure_viewfinder(asd,
5109 							       f->fmt.pix.width, f->fmt.pix.height,
5110 							       format_bridge->planar ? f->fmt.pix.bytesperline
5111 							       : f->fmt.pix.bytesperline * 8
5112 							       / format_bridge->depth,	format_bridge->sh_fmt);
5113 			atomisp_css_video_get_viewfinder_frame_info(asd,
5114 				&output_info);
5115 			asd->copy_mode = false;
5116 		} else {
5117 			atomisp_css_capture_configure_viewfinder(asd,
5118 				f->fmt.pix.width, f->fmt.pix.height,
5119 				format_bridge->planar ? f->fmt.pix.bytesperline
5120 				: f->fmt.pix.bytesperline * 8
5121 				/ format_bridge->depth,	format_bridge->sh_fmt);
5122 			atomisp_css_capture_get_viewfinder_frame_info(asd,
5123 				&output_info);
5124 			asd->copy_mode = false;
5125 		}
5126 
5127 		goto done;
5128 	}
5129 	/*
5130 	 * Check whether main resolution configured smaller
5131 	 * than snapshot resolution. If so, force main resolution
5132 	 * to be the same as snapshot resolution
5133 	 */
5134 	if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE) {
5135 		struct v4l2_rect *r;
5136 
5137 		r = atomisp_subdev_get_rect(
5138 			&asd->subdev, NULL,
5139 			V4L2_SUBDEV_FORMAT_ACTIVE,
5140 			ATOMISP_SUBDEV_PAD_SOURCE_VF, V4L2_SEL_TGT_COMPOSE);
5141 
5142 		if (r->width && r->height
5143 		    && (r->width > f->fmt.pix.width
5144 			|| r->height > f->fmt.pix.height))
5145 			dev_warn(isp->dev,
5146 				 "Main Resolution config smaller then Vf Resolution. Force to be equal with Vf Resolution.");
5147 	}
5148 
5149 	/* Pipeline configuration done through subdevs. Bail out now. */
5150 	if (!asd->fmt_auto->val)
5151 		goto set_fmt_to_isp;
5152 
5153 	/* get sensor resolution and format */
5154 	ret = atomisp_try_fmt(vdev, &snr_fmt, &res_overflow);
5155 	if (ret) {
5156 		dev_warn(isp->dev, "Try format failed with error %d\n", ret);
5157 		return ret;
5158 	}
5159 	f->fmt.pix.width = snr_fmt.width;
5160 	f->fmt.pix.height = snr_fmt.height;
5161 
5162 	snr_format_bridge = atomisp_get_format_bridge(snr_fmt.pixelformat);
5163 	if (!snr_format_bridge) {
5164 		dev_warn(isp->dev, "Can't find bridge format\n");
5165 		return -EINVAL;
5166 	}
5167 
5168 	atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5169 				V4L2_SUBDEV_FORMAT_ACTIVE,
5170 				ATOMISP_SUBDEV_PAD_SINK)->code =
5171 				    snr_format_bridge->mbus_code;
5172 
5173 	isp_sink_fmt = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5174 						V4L2_SUBDEV_FORMAT_ACTIVE,
5175 						ATOMISP_SUBDEV_PAD_SINK);
5176 
5177 	isp_source_fmt.code = format_bridge->mbus_code;
5178 	atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
5179 				V4L2_SUBDEV_FORMAT_ACTIVE,
5180 				source_pad, &isp_source_fmt);
5181 
5182 	if (!atomisp_subdev_format_conversion(asd, source_pad)) {
5183 		padding_w = 0;
5184 		padding_h = 0;
5185 	} else if (IS_BYT) {
5186 		padding_w = 12;
5187 		padding_h = 12;
5188 	}
5189 
5190 	/* construct resolution supported by isp */
5191 	if (res_overflow && !asd->continuous_mode->val) {
5192 		f->fmt.pix.width = rounddown(
5193 				       clamp_t(u32, f->fmt.pix.width - padding_w,
5194 					       ATOM_ISP_MIN_WIDTH,
5195 					       ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
5196 		f->fmt.pix.height = rounddown(
5197 					clamp_t(u32, f->fmt.pix.height - padding_h,
5198 						ATOM_ISP_MIN_HEIGHT,
5199 						ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
5200 	}
5201 
5202 	atomisp_get_dis_envelop(asd, f->fmt.pix.width, f->fmt.pix.height,
5203 				&dvs_env_w, &dvs_env_h);
5204 
5205 	if (asd->continuous_mode->val) {
5206 		struct v4l2_rect *r;
5207 
5208 		r = atomisp_subdev_get_rect(
5209 			&asd->subdev, NULL,
5210 			V4L2_SUBDEV_FORMAT_ACTIVE,
5211 			ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE,
5212 			V4L2_SEL_TGT_COMPOSE);
5213 		/*
5214 		 * The ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE should get resolutions
5215 		 * properly set otherwise, it should not be the capture_pad.
5216 		 */
5217 		if (r->width && r->height)
5218 			asd->capture_pad = ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE;
5219 		else
5220 			asd->capture_pad = source_pad;
5221 	} else {
5222 		asd->capture_pad = source_pad;
5223 	}
5224 	/*
5225 	 * set format info to sensor
5226 	 * In continuous mode, resolution is set only if it is higher than
5227 	 * existing value. This because preview pipe will be configured after
5228 	 * capture pipe and usually has lower resolution than capture pipe.
5229 	 */
5230 	if (!asd->continuous_mode->val ||
5231 	    isp_sink_fmt->width < (f->fmt.pix.width + padding_w + dvs_env_w) ||
5232 	    isp_sink_fmt->height < (f->fmt.pix.height + padding_h +
5233 				    dvs_env_h)) {
5234 		/*
5235 		 * For jpeg or custom raw format the sensor will return constant
5236 		 * width and height. Because we already had quried try_mbus_fmt,
5237 		 * f->fmt.pix.width and f->fmt.pix.height has been changed to
5238 		 * this fixed width and height. So we cannot select the correct
5239 		 * resolution with that information. So use the original width
5240 		 * and height while set_mbus_fmt() so actual resolutions are
5241 		 * being used in while set media bus format.
5242 		 */
5243 		s_fmt = f->fmt.pix;
5244 		if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG ||
5245 		    f->fmt.pix.pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW) {
5246 			s_fmt.width = backup_fmt.width;
5247 			s_fmt.height = backup_fmt.height;
5248 		}
5249 		ret = atomisp_set_fmt_to_snr(vdev, &s_fmt,
5250 					     f->fmt.pix.pixelformat, padding_w,
5251 					     padding_h, dvs_env_w, dvs_env_h);
5252 		if (ret) {
5253 			dev_warn(isp->dev,
5254 				 "Set format to sensor failed with %d\n", ret);
5255 			return -EINVAL;
5256 		}
5257 
5258 		atomisp_csi_lane_config(isp);
5259 		crop_needs_override = true;
5260 	}
5261 
5262 	atomisp_check_copy_mode(asd, source_pad, &backup_fmt);
5263 	asd->yuvpp_mode = false;			/* Reset variable */
5264 
5265 	isp_sink_crop = *atomisp_subdev_get_rect(&asd->subdev, NULL,
5266 			V4L2_SUBDEV_FORMAT_ACTIVE,
5267 			ATOMISP_SUBDEV_PAD_SINK,
5268 			V4L2_SEL_TGT_CROP);
5269 
5270 	/* Try to enable YUV downscaling if ISP input is 10 % (either
5271 	 * width or height) bigger than the desired result. */
5272 	if (isp_sink_crop.width * 9 / 10 < f->fmt.pix.width ||
5273 	    isp_sink_crop.height * 9 / 10 < f->fmt.pix.height ||
5274 	    (atomisp_subdev_format_conversion(asd, source_pad) &&
5275 	     ((asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
5276 	       !asd->continuous_mode->val) ||
5277 	      asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER))) {
5278 		/* for continuous mode, preview size might be smaller than
5279 		 * still capture size. if preview size still needs crop,
5280 		 * pick the larger one between crop size of preview and
5281 		 * still capture.
5282 		 */
5283 		if (asd->continuous_mode->val
5284 		    && source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW
5285 		    && !crop_needs_override) {
5286 			isp_sink_crop.width =
5287 			    max_t(unsigned int, f->fmt.pix.width,
5288 				  isp_sink_crop.width);
5289 			isp_sink_crop.height =
5290 			    max_t(unsigned int, f->fmt.pix.height,
5291 				  isp_sink_crop.height);
5292 		} else {
5293 			isp_sink_crop.width = f->fmt.pix.width;
5294 			isp_sink_crop.height = f->fmt.pix.height;
5295 		}
5296 
5297 		atomisp_subdev_set_selection(&asd->subdev, fh.state,
5298 					     V4L2_SUBDEV_FORMAT_ACTIVE,
5299 					     ATOMISP_SUBDEV_PAD_SINK,
5300 					     V4L2_SEL_TGT_CROP,
5301 					     V4L2_SEL_FLAG_KEEP_CONFIG,
5302 					     &isp_sink_crop);
5303 		atomisp_subdev_set_selection(&asd->subdev, fh.state,
5304 					     V4L2_SUBDEV_FORMAT_ACTIVE,
5305 					     source_pad, V4L2_SEL_TGT_COMPOSE,
5306 					     0, &isp_sink_crop);
5307 	} else if (IS_MOFD) {
5308 		struct v4l2_rect main_compose = {0};
5309 
5310 		main_compose.width = isp_sink_crop.width;
5311 		main_compose.height =
5312 		    DIV_ROUND_UP(main_compose.width * f->fmt.pix.height,
5313 				 f->fmt.pix.width);
5314 		if (main_compose.height > isp_sink_crop.height) {
5315 			main_compose.height = isp_sink_crop.height;
5316 			main_compose.width =
5317 			    DIV_ROUND_UP(main_compose.height *
5318 					 f->fmt.pix.width,
5319 					 f->fmt.pix.height);
5320 		}
5321 
5322 		atomisp_subdev_set_selection(&asd->subdev, fh.state,
5323 					     V4L2_SUBDEV_FORMAT_ACTIVE,
5324 					     source_pad,
5325 					     V4L2_SEL_TGT_COMPOSE, 0,
5326 					     &main_compose);
5327 	} else {
5328 		struct v4l2_rect sink_crop = {0};
5329 		struct v4l2_rect main_compose = {0};
5330 
5331 		main_compose.width = f->fmt.pix.width;
5332 		main_compose.height = f->fmt.pix.height;
5333 
5334 		/* WORKAROUND: this override is universally enabled in
5335 		 * GMIN to work around a CTS failures (GMINL-539)
5336 		 * which appears to be related by a hardware
5337 		 * performance limitation.  It's unclear why this
5338 		 * particular code triggers the issue. */
5339 		if (crop_needs_override) {
5340 			if (isp_sink_crop.width * main_compose.height >
5341 			    isp_sink_crop.height * main_compose.width) {
5342 				sink_crop.height = isp_sink_crop.height;
5343 				sink_crop.width = DIV_NEAREST_STEP(
5344 						      sink_crop.height *
5345 						      f->fmt.pix.width,
5346 						      f->fmt.pix.height,
5347 						      ATOM_ISP_STEP_WIDTH);
5348 			} else {
5349 				sink_crop.width = isp_sink_crop.width;
5350 				sink_crop.height = DIV_NEAREST_STEP(
5351 						       sink_crop.width *
5352 						       f->fmt.pix.height,
5353 						       f->fmt.pix.width,
5354 						       ATOM_ISP_STEP_HEIGHT);
5355 			}
5356 			atomisp_subdev_set_selection(&asd->subdev, fh.state,
5357 						     V4L2_SUBDEV_FORMAT_ACTIVE,
5358 						     ATOMISP_SUBDEV_PAD_SINK,
5359 						     V4L2_SEL_TGT_CROP,
5360 						     V4L2_SEL_FLAG_KEEP_CONFIG,
5361 						     &sink_crop);
5362 		}
5363 		atomisp_subdev_set_selection(&asd->subdev, fh.state,
5364 					     V4L2_SUBDEV_FORMAT_ACTIVE,
5365 					     source_pad,
5366 					     V4L2_SEL_TGT_COMPOSE, 0,
5367 					     &main_compose);
5368 	}
5369 
5370 set_fmt_to_isp:
5371 	ret = atomisp_set_fmt_to_isp(vdev, &output_info, &raw_output_info,
5372 				     &f->fmt.pix, source_pad);
5373 	if (ret) {
5374 		dev_warn(isp->dev, "Can't set format on ISP. Error %d\n", ret);
5375 		return -EINVAL;
5376 	}
5377 done:
5378 	pipe->pix.width = f->fmt.pix.width;
5379 	pipe->pix.height = f->fmt.pix.height;
5380 	pipe->pix.pixelformat = f->fmt.pix.pixelformat;
5381 	/*
5382 	 * FIXME: do we need to setup this differently, depending on the
5383 	 * sensor or the pipeline?
5384 	 */
5385 	pipe->pix.colorspace = V4L2_COLORSPACE_REC709;
5386 	pipe->pix.ycbcr_enc = V4L2_YCBCR_ENC_709;
5387 	pipe->pix.xfer_func = V4L2_XFER_FUNC_709;
5388 
5389 	if (format_bridge->planar) {
5390 		pipe->pix.bytesperline = output_info.padded_width;
5391 		pipe->pix.sizeimage = PAGE_ALIGN(f->fmt.pix.height *
5392 						 DIV_ROUND_UP(format_bridge->depth *
5393 							 output_info.padded_width, 8));
5394 	} else {
5395 		pipe->pix.bytesperline =
5396 		    DIV_ROUND_UP(format_bridge->depth *
5397 				 output_info.padded_width, 8);
5398 		pipe->pix.sizeimage =
5399 		    PAGE_ALIGN(f->fmt.pix.height * pipe->pix.bytesperline);
5400 	}
5401 	dev_dbg(isp->dev, "%s: image size: %d, %d bytes per line\n",
5402 		__func__, pipe->pix.sizeimage, pipe->pix.bytesperline);
5403 
5404 	if (f->fmt.pix.field == V4L2_FIELD_ANY)
5405 		f->fmt.pix.field = V4L2_FIELD_NONE;
5406 	pipe->pix.field = f->fmt.pix.field;
5407 
5408 	f->fmt.pix = pipe->pix;
5409 	f->fmt.pix.priv = PAGE_ALIGN(pipe->pix.width *
5410 				     pipe->pix.height * 2);
5411 
5412 	/*
5413 	 * If in video 480P case, no GFX throttle
5414 	 */
5415 	if (asd->run_mode->val == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO &&
5416 	    f->fmt.pix.width == 720 && f->fmt.pix.height == 480)
5417 		isp->need_gfx_throttle = false;
5418 	else
5419 		isp->need_gfx_throttle = true;
5420 
5421 	/* Report the needed sizes */
5422 	f->fmt.pix.sizeimage = pipe->pix.sizeimage;
5423 	f->fmt.pix.bytesperline = pipe->pix.bytesperline;
5424 
5425 	dev_dbg(isp->dev, "%s: %dx%d, image size: %d, %d bytes per line\n",
5426 		__func__,
5427 		f->fmt.pix.width, f->fmt.pix.height,
5428 		f->fmt.pix.sizeimage, f->fmt.pix.bytesperline);
5429 
5430 	return 0;
5431 }
5432 
5433 int atomisp_set_shading_table(struct atomisp_sub_device *asd,
5434 			      struct atomisp_shading_table *user_shading_table)
5435 {
5436 	struct ia_css_shading_table *shading_table;
5437 	struct ia_css_shading_table *free_table;
5438 	unsigned int len_table;
5439 	int i;
5440 	int ret = 0;
5441 
5442 	if (!user_shading_table)
5443 		return -EINVAL;
5444 
5445 	if (!user_shading_table->enable) {
5446 		asd->params.config.shading_table = NULL;
5447 		asd->params.sc_en = false;
5448 		return 0;
5449 	}
5450 
5451 	/* If enabling, all tables must be set */
5452 	for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
5453 		if (!user_shading_table->data[i])
5454 			return -EINVAL;
5455 	}
5456 
5457 	/* Shading table size per color */
5458 	if (user_shading_table->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
5459 	    user_shading_table->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR)
5460 		return -EINVAL;
5461 
5462 	shading_table = atomisp_css_shading_table_alloc(
5463 			    user_shading_table->width, user_shading_table->height);
5464 	if (!shading_table)
5465 		return -ENOMEM;
5466 
5467 	len_table = user_shading_table->width * user_shading_table->height *
5468 		    ATOMISP_SC_TYPE_SIZE;
5469 	for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
5470 		ret = copy_from_user(shading_table->data[i],
5471 				     (void __user *)user_shading_table->data[i],
5472 				     len_table);
5473 		if (ret) {
5474 			free_table = shading_table;
5475 			ret = -EFAULT;
5476 			goto out;
5477 		}
5478 	}
5479 	shading_table->sensor_width = user_shading_table->sensor_width;
5480 	shading_table->sensor_height = user_shading_table->sensor_height;
5481 	shading_table->fraction_bits = user_shading_table->fraction_bits;
5482 
5483 	free_table = asd->params.css_param.shading_table;
5484 	asd->params.css_param.shading_table = shading_table;
5485 	asd->params.config.shading_table = shading_table;
5486 	asd->params.sc_en = true;
5487 
5488 out:
5489 	if (free_table)
5490 		atomisp_css_shading_table_free(free_table);
5491 
5492 	return ret;
5493 }
5494 
5495 int atomisp_exif_makernote(struct atomisp_sub_device *asd,
5496 			   struct atomisp_makernote_info *config)
5497 {
5498 	struct v4l2_control ctrl;
5499 	struct atomisp_device *isp = asd->isp;
5500 
5501 	ctrl.id = V4L2_CID_FOCAL_ABSOLUTE;
5502 	if (v4l2_g_ctrl
5503 	    (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
5504 		dev_warn(isp->dev, "failed to g_ctrl for focal length\n");
5505 		return -EINVAL;
5506 	} else {
5507 		config->focal_length = ctrl.value;
5508 	}
5509 
5510 	ctrl.id = V4L2_CID_FNUMBER_ABSOLUTE;
5511 	if (v4l2_g_ctrl
5512 	    (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
5513 		dev_warn(isp->dev, "failed to g_ctrl for f-number\n");
5514 		return -EINVAL;
5515 	} else {
5516 		config->f_number_curr = ctrl.value;
5517 	}
5518 
5519 	ctrl.id = V4L2_CID_FNUMBER_RANGE;
5520 	if (v4l2_g_ctrl
5521 	    (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
5522 		dev_warn(isp->dev, "failed to g_ctrl for f number range\n");
5523 		return -EINVAL;
5524 	} else {
5525 		config->f_number_range = ctrl.value;
5526 	}
5527 
5528 	return 0;
5529 }
5530 
5531 int atomisp_offline_capture_configure(struct atomisp_sub_device *asd,
5532 				      struct atomisp_cont_capture_conf *cvf_config)
5533 {
5534 	struct v4l2_ctrl *c;
5535 
5536 	lockdep_assert_held(&asd->isp->mutex);
5537 
5538 	/*
5539 	* In case of M10MO ZSL capture case, we need to issue a separate
5540 	* capture request to M10MO which will output captured jpeg image
5541 	*/
5542 	c = v4l2_ctrl_find(
5543 		asd->isp->inputs[asd->input_curr].camera->ctrl_handler,
5544 		V4L2_CID_START_ZSL_CAPTURE);
5545 	if (c) {
5546 		int ret;
5547 
5548 		dev_dbg(asd->isp->dev, "%s trigger ZSL capture request\n",
5549 			__func__);
5550 		/* TODO: use the cvf_config */
5551 		ret = v4l2_ctrl_s_ctrl(c, 1);
5552 		if (ret)
5553 			return ret;
5554 
5555 		return v4l2_ctrl_s_ctrl(c, 0);
5556 	}
5557 
5558 	asd->params.offline_parm = *cvf_config;
5559 
5560 	if (asd->params.offline_parm.num_captures) {
5561 		if (asd->streaming == ATOMISP_DEVICE_STREAMING_DISABLED) {
5562 			unsigned int init_raw_num;
5563 
5564 			if (asd->enable_raw_buffer_lock->val) {
5565 				init_raw_num =
5566 				    ATOMISP_CSS2_NUM_OFFLINE_INIT_CONTINUOUS_FRAMES_LOCK_EN;
5567 				if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
5568 				    asd->params.video_dis_en)
5569 					init_raw_num +=
5570 					    ATOMISP_CSS2_NUM_DVS_FRAME_DELAY;
5571 			} else {
5572 				init_raw_num =
5573 				    ATOMISP_CSS2_NUM_OFFLINE_INIT_CONTINUOUS_FRAMES;
5574 			}
5575 
5576 			/* TODO: this can be removed once user-space
5577 			 *       has been updated to use control API */
5578 			asd->continuous_raw_buffer_size->val =
5579 			    max_t(int,
5580 				  asd->continuous_raw_buffer_size->val,
5581 				  asd->params.offline_parm.
5582 				  num_captures + init_raw_num);
5583 			asd->continuous_raw_buffer_size->val =
5584 			    min_t(int, ATOMISP_CONT_RAW_FRAMES,
5585 				  asd->continuous_raw_buffer_size->val);
5586 		}
5587 		asd->continuous_mode->val = true;
5588 	} else {
5589 		asd->continuous_mode->val = false;
5590 		__enable_continuous_mode(asd, false);
5591 	}
5592 
5593 	return 0;
5594 }
5595 
5596 /*
5597  * set auto exposure metering window to camera sensor
5598  */
5599 int atomisp_s_ae_window(struct atomisp_sub_device *asd,
5600 			struct atomisp_ae_window *arg)
5601 {
5602 	struct atomisp_device *isp = asd->isp;
5603 	/* Coverity CID 298071 - initialzize struct */
5604 	struct v4l2_subdev_selection sel = { 0 };
5605 
5606 	sel.r.left = arg->x_left;
5607 	sel.r.top = arg->y_top;
5608 	sel.r.width = arg->x_right - arg->x_left + 1;
5609 	sel.r.height = arg->y_bottom - arg->y_top + 1;
5610 
5611 	if (v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
5612 			     pad, set_selection, NULL, &sel)) {
5613 		dev_err(isp->dev, "failed to call sensor set_selection.\n");
5614 		return -EINVAL;
5615 	}
5616 
5617 	return 0;
5618 }
5619 
5620 int atomisp_flash_enable(struct atomisp_sub_device *asd, int num_frames)
5621 {
5622 	struct atomisp_device *isp = asd->isp;
5623 
5624 	if (num_frames < 0) {
5625 		dev_dbg(isp->dev, "%s ERROR: num_frames: %d\n", __func__,
5626 			num_frames);
5627 		return -EINVAL;
5628 	}
5629 	/* a requested flash is still in progress. */
5630 	if (num_frames && asd->params.flash_state != ATOMISP_FLASH_IDLE) {
5631 		dev_dbg(isp->dev, "%s flash busy: %d frames left: %d\n",
5632 			__func__, asd->params.flash_state,
5633 			asd->params.num_flash_frames);
5634 		return -EBUSY;
5635 	}
5636 
5637 	asd->params.num_flash_frames = num_frames;
5638 	asd->params.flash_state = ATOMISP_FLASH_REQUESTED;
5639 	return 0;
5640 }
5641 
5642 bool atomisp_is_vf_pipe(struct atomisp_video_pipe *pipe)
5643 {
5644 	struct atomisp_sub_device *asd = pipe->asd;
5645 
5646 	if (!asd) {
5647 		dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
5648 			__func__, pipe->vdev.name);
5649 		return false;
5650 	}
5651 
5652 	if (pipe == &asd->video_out_vf)
5653 		return true;
5654 
5655 	if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
5656 	    pipe == &asd->video_out_preview)
5657 		return true;
5658 
5659 	return false;
5660 }
5661 
5662 static int __checking_exp_id(struct atomisp_sub_device *asd, int exp_id)
5663 {
5664 	struct atomisp_device *isp = asd->isp;
5665 
5666 	if (!asd->enable_raw_buffer_lock->val) {
5667 		dev_warn(isp->dev, "%s Raw Buffer Lock is disable.\n", __func__);
5668 		return -EINVAL;
5669 	}
5670 	if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED) {
5671 		dev_err(isp->dev, "%s streaming %d invalid exp_id %d.\n",
5672 			__func__, exp_id, asd->streaming);
5673 		return -EINVAL;
5674 	}
5675 	if ((exp_id > ATOMISP_MAX_EXP_ID) || (exp_id <= 0)) {
5676 		dev_err(isp->dev, "%s exp_id %d invalid.\n", __func__, exp_id);
5677 		return -EINVAL;
5678 	}
5679 	return 0;
5680 }
5681 
5682 void atomisp_init_raw_buffer_bitmap(struct atomisp_sub_device *asd)
5683 {
5684 	unsigned long flags;
5685 
5686 	spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
5687 	memset(asd->raw_buffer_bitmap, 0, sizeof(asd->raw_buffer_bitmap));
5688 	asd->raw_buffer_locked_count = 0;
5689 	spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
5690 }
5691 
5692 static int atomisp_set_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
5693 {
5694 	int *bitmap, bit;
5695 	unsigned long flags;
5696 
5697 	if (__checking_exp_id(asd, exp_id))
5698 		return -EINVAL;
5699 
5700 	bitmap = asd->raw_buffer_bitmap + exp_id / 32;
5701 	bit = exp_id % 32;
5702 	spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
5703 	(*bitmap) |= (1 << bit);
5704 	asd->raw_buffer_locked_count++;
5705 	spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
5706 
5707 	dev_dbg(asd->isp->dev, "%s: exp_id %d,  raw_buffer_locked_count %d\n",
5708 		__func__, exp_id, asd->raw_buffer_locked_count);
5709 
5710 	/* Check if the raw buffer after next is still locked!!! */
5711 	exp_id += 2;
5712 	if (exp_id > ATOMISP_MAX_EXP_ID)
5713 		exp_id -= ATOMISP_MAX_EXP_ID;
5714 	bitmap = asd->raw_buffer_bitmap + exp_id / 32;
5715 	bit = exp_id % 32;
5716 	if ((*bitmap) & (1 << bit)) {
5717 		int ret;
5718 
5719 		/* WORKAROUND unlock the raw buffer compulsively */
5720 		ret = atomisp_css_exp_id_unlock(asd, exp_id);
5721 		if (ret) {
5722 			dev_err(asd->isp->dev,
5723 				"%s exp_id is wrapping back to %d but force unlock failed, err %d.\n",
5724 				__func__, exp_id, ret);
5725 			return ret;
5726 		}
5727 
5728 		spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
5729 		(*bitmap) &= ~(1 << bit);
5730 		asd->raw_buffer_locked_count--;
5731 		spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
5732 		dev_warn(asd->isp->dev,
5733 			 "%s exp_id is wrapping back to %d but it is still locked so force unlock it, raw_buffer_locked_count %d\n",
5734 			 __func__, exp_id, asd->raw_buffer_locked_count);
5735 	}
5736 	return 0;
5737 }
5738 
5739 static int __is_raw_buffer_locked(struct atomisp_sub_device *asd, int exp_id)
5740 {
5741 	int *bitmap, bit;
5742 	unsigned long flags;
5743 	int ret;
5744 
5745 	if (__checking_exp_id(asd, exp_id))
5746 		return -EINVAL;
5747 
5748 	bitmap = asd->raw_buffer_bitmap + exp_id / 32;
5749 	bit = exp_id % 32;
5750 	spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
5751 	ret = ((*bitmap) & (1 << bit));
5752 	spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
5753 	return !ret;
5754 }
5755 
5756 static int __clear_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
5757 {
5758 	int *bitmap, bit;
5759 	unsigned long flags;
5760 
5761 	if (__is_raw_buffer_locked(asd, exp_id))
5762 		return -EINVAL;
5763 
5764 	bitmap = asd->raw_buffer_bitmap + exp_id / 32;
5765 	bit = exp_id % 32;
5766 	spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
5767 	(*bitmap) &= ~(1 << bit);
5768 	asd->raw_buffer_locked_count--;
5769 	spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
5770 
5771 	dev_dbg(asd->isp->dev, "%s: exp_id %d,  raw_buffer_locked_count %d\n",
5772 		__func__, exp_id, asd->raw_buffer_locked_count);
5773 	return 0;
5774 }
5775 
5776 int atomisp_exp_id_capture(struct atomisp_sub_device *asd, int *exp_id)
5777 {
5778 	struct atomisp_device *isp = asd->isp;
5779 	int value = *exp_id;
5780 	int ret;
5781 
5782 	lockdep_assert_held(&isp->mutex);
5783 
5784 	ret = __is_raw_buffer_locked(asd, value);
5785 	if (ret) {
5786 		dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
5787 		return -EINVAL;
5788 	}
5789 
5790 	dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
5791 	ret = atomisp_css_exp_id_capture(asd, value);
5792 	if (ret) {
5793 		dev_err(isp->dev, "%s exp_id %d failed.\n", __func__, value);
5794 		return -EIO;
5795 	}
5796 	return 0;
5797 }
5798 
5799 int atomisp_exp_id_unlock(struct atomisp_sub_device *asd, int *exp_id)
5800 {
5801 	struct atomisp_device *isp = asd->isp;
5802 	int value = *exp_id;
5803 	int ret;
5804 
5805 	lockdep_assert_held(&isp->mutex);
5806 
5807 	ret = __clear_raw_buffer_bitmap(asd, value);
5808 	if (ret) {
5809 		dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
5810 		return -EINVAL;
5811 	}
5812 
5813 	dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
5814 	ret = atomisp_css_exp_id_unlock(asd, value);
5815 	if (ret)
5816 		dev_err(isp->dev, "%s exp_id %d failed, err %d.\n",
5817 			__func__, value, ret);
5818 
5819 	return ret;
5820 }
5821 
5822 int atomisp_enable_dz_capt_pipe(struct atomisp_sub_device *asd,
5823 				unsigned int *enable)
5824 {
5825 	bool value;
5826 
5827 	if (!enable)
5828 		return -EINVAL;
5829 
5830 	value = *enable > 0;
5831 
5832 	atomisp_en_dz_capt_pipe(asd, value);
5833 
5834 	return 0;
5835 }
5836 
5837 int atomisp_inject_a_fake_event(struct atomisp_sub_device *asd, int *event)
5838 {
5839 	if (!event || asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
5840 		return -EINVAL;
5841 
5842 	lockdep_assert_held(&asd->isp->mutex);
5843 
5844 	dev_dbg(asd->isp->dev, "%s: trying to inject a fake event 0x%x\n",
5845 		__func__, *event);
5846 
5847 	switch (*event) {
5848 	case V4L2_EVENT_FRAME_SYNC:
5849 		atomisp_sof_event(asd);
5850 		break;
5851 	case V4L2_EVENT_FRAME_END:
5852 		atomisp_eof_event(asd, 0);
5853 		break;
5854 	case V4L2_EVENT_ATOMISP_3A_STATS_READY:
5855 		atomisp_3a_stats_ready_event(asd, 0);
5856 		break;
5857 	case V4L2_EVENT_ATOMISP_METADATA_READY:
5858 		atomisp_metadata_ready_event(asd, 0);
5859 		break;
5860 	default:
5861 		return -EINVAL;
5862 	}
5863 
5864 	return 0;
5865 }
5866 
5867 static int atomisp_get_pipe_id(struct atomisp_video_pipe *pipe)
5868 {
5869 	struct atomisp_sub_device *asd = pipe->asd;
5870 
5871 	if (!asd) {
5872 		dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
5873 			__func__, pipe->vdev.name);
5874 		return -EINVAL;
5875 	}
5876 
5877 	if (ATOMISP_USE_YUVPP(asd)) {
5878 		return IA_CSS_PIPE_ID_YUVPP;
5879 	} else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
5880 		return IA_CSS_PIPE_ID_VIDEO;
5881 	} else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
5882 		return IA_CSS_PIPE_ID_CAPTURE;
5883 	} else if (pipe == &asd->video_out_video_capture) {
5884 		return IA_CSS_PIPE_ID_VIDEO;
5885 	} else if (pipe == &asd->video_out_vf) {
5886 		return IA_CSS_PIPE_ID_CAPTURE;
5887 	} else if (pipe == &asd->video_out_preview) {
5888 		if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
5889 			return IA_CSS_PIPE_ID_VIDEO;
5890 		else
5891 			return IA_CSS_PIPE_ID_PREVIEW;
5892 	} else if (pipe == &asd->video_out_capture) {
5893 		if (asd->copy_mode)
5894 			return IA_CSS_PIPE_ID_COPY;
5895 		else
5896 			return IA_CSS_PIPE_ID_CAPTURE;
5897 	}
5898 
5899 	/* fail through */
5900 	dev_warn(asd->isp->dev, "%s failed to find proper pipe\n",
5901 		 __func__);
5902 	return IA_CSS_PIPE_ID_CAPTURE;
5903 }
5904 
5905 int atomisp_get_invalid_frame_num(struct video_device *vdev,
5906 				  int *invalid_frame_num)
5907 {
5908 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
5909 	struct atomisp_sub_device *asd = pipe->asd;
5910 	enum ia_css_pipe_id pipe_id;
5911 	struct ia_css_pipe_info p_info;
5912 	int ret;
5913 
5914 	pipe_id = atomisp_get_pipe_id(pipe);
5915 	if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipes[pipe_id]) {
5916 		dev_warn(asd->isp->dev,
5917 			 "%s pipe %d has not been created yet, do SET_FMT first!\n",
5918 			 __func__, pipe_id);
5919 		return -EINVAL;
5920 	}
5921 
5922 	ret = ia_css_pipe_get_info(
5923 		  asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
5924 		  .pipes[pipe_id], &p_info);
5925 	if (!ret) {
5926 		*invalid_frame_num = p_info.num_invalid_frames;
5927 		return 0;
5928 	} else {
5929 		dev_warn(asd->isp->dev, "%s get pipe infor failed %d\n",
5930 			 __func__, ret);
5931 		return -EINVAL;
5932 	}
5933 }
5934