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