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