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