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 #include <linux/errno.h>
10 #include <linux/firmware.h>
11 #include <linux/pci.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/kernel.h>
15 #include <linux/kfifo.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/timer.h>
18 
19 #include <asm/iosf_mbi.h>
20 
21 #include <media/v4l2-event.h>
22 
23 #define CREATE_TRACE_POINTS
24 #include "atomisp_trace_event.h"
25 
26 #include "atomisp_cmd.h"
27 #include "atomisp_common.h"
28 #include "atomisp_fops.h"
29 #include "atomisp_internal.h"
30 #include "atomisp_ioctl.h"
31 #include "atomisp-regs.h"
32 #include "atomisp_tables.h"
33 #include "atomisp_compat.h"
34 #include "atomisp_subdev.h"
35 #include "atomisp_dfs_tables.h"
36 
37 #include <hmm/hmm.h>
38 
39 #include "sh_css_hrt.h"
40 #include "sh_css_defs.h"
41 #include "system_global.h"
42 #include "sh_css_internal.h"
43 #include "sh_css_sp.h"
44 #include "gp_device.h"
45 #include "device_access.h"
46 #include "irq.h"
47 
48 #include "ia_css_types.h"
49 #include "ia_css_stream.h"
50 #include "ia_css_debug.h"
51 #include "bits.h"
52 
53 union host {
54 	struct {
55 		void *kernel_ptr;
56 		void __user *user_ptr;
57 		int size;
58 	} scalar;
59 	struct {
60 		void *hmm_ptr;
61 	} ptr;
62 };
63 
64 /*
65  * get sensor:dis71430/ov2720 related info from v4l2_subdev->priv data field.
66  * subdev->priv is set in mrst.c
67  */
atomisp_to_sensor_mipi_info(struct v4l2_subdev * sd)68 struct camera_mipi_info *atomisp_to_sensor_mipi_info(struct v4l2_subdev *sd)
69 {
70 	return (struct camera_mipi_info *)v4l2_get_subdev_hostdata(sd);
71 }
72 
73 /*
74  * get struct atomisp_video_pipe from v4l2 video_device
75  */
atomisp_to_video_pipe(struct video_device * dev)76 struct atomisp_video_pipe *atomisp_to_video_pipe(struct video_device *dev)
77 {
78 	return (struct atomisp_video_pipe *)
79 	       container_of(dev, struct atomisp_video_pipe, vdev);
80 }
81 
atomisp_get_sensor_fps(struct atomisp_sub_device * asd)82 static unsigned short atomisp_get_sensor_fps(struct atomisp_sub_device *asd)
83 {
84 	struct v4l2_subdev_frame_interval fi = { 0 };
85 	struct atomisp_device *isp = asd->isp;
86 
87 	unsigned short fps = 0;
88 	int ret;
89 
90 	ret = v4l2_subdev_call_state_active(isp->inputs[asd->input_curr].camera,
91 					    pad, get_frame_interval, &fi);
92 
93 	if (!ret && fi.interval.numerator)
94 		fps = fi.interval.denominator / fi.interval.numerator;
95 
96 	return fps;
97 }
98 
99 /*
100  * DFS progress is shown as follows:
101  * 1. Target frequency is calculated according to FPS/Resolution/ISP running
102  *    mode.
103  * 2. Ratio is calculated using formula: 2 * HPLL / target frequency - 1
104  *    with proper rounding.
105  * 3. Set ratio to ISPFREQ40, 1 to FREQVALID and ISPFREQGUAR40
106  *    to 200MHz in ISPSSPM1.
107  * 4. Wait for FREQVALID to be cleared by P-Unit.
108  * 5. Wait for field ISPFREQSTAT40 in ISPSSPM1 turn to ratio set in 3.
109  */
write_target_freq_to_hw(struct atomisp_device * isp,unsigned int new_freq)110 static int write_target_freq_to_hw(struct atomisp_device *isp,
111 				   unsigned int new_freq)
112 {
113 	unsigned int ratio, timeout, guar_ratio;
114 	u32 isp_sspm1 = 0;
115 	int i;
116 
117 	if (!isp->hpll_freq) {
118 		dev_err(isp->dev, "failed to get hpll_freq. no change to freq\n");
119 		return -EINVAL;
120 	}
121 
122 	iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
123 	if (isp_sspm1 & ISP_FREQ_VALID_MASK) {
124 		dev_dbg(isp->dev, "clearing ISPSSPM1 valid bit.\n");
125 		iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1,
126 			       isp_sspm1 & ~(1 << ISP_FREQ_VALID_OFFSET));
127 	}
128 
129 	ratio = (2 * isp->hpll_freq + new_freq / 2) / new_freq - 1;
130 	guar_ratio = (2 * isp->hpll_freq + 200 / 2) / 200 - 1;
131 
132 	iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
133 	isp_sspm1 &= ~(0x1F << ISP_REQ_FREQ_OFFSET);
134 
135 	for (i = 0; i < ISP_DFS_TRY_TIMES; i++) {
136 		iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1,
137 			       isp_sspm1
138 			       | ratio << ISP_REQ_FREQ_OFFSET
139 			       | 1 << ISP_FREQ_VALID_OFFSET
140 			       | guar_ratio << ISP_REQ_GUAR_FREQ_OFFSET);
141 
142 		iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
143 		timeout = 20;
144 		while ((isp_sspm1 & ISP_FREQ_VALID_MASK) && timeout) {
145 			iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
146 			dev_dbg(isp->dev, "waiting for ISPSSPM1 valid bit to be 0.\n");
147 			udelay(100);
148 			timeout--;
149 		}
150 
151 		if (timeout != 0)
152 			break;
153 	}
154 
155 	if (timeout == 0) {
156 		dev_err(isp->dev, "DFS failed due to HW error.\n");
157 		return -EINVAL;
158 	}
159 
160 	iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
161 	timeout = 10;
162 	while (((isp_sspm1 >> ISP_FREQ_STAT_OFFSET) != ratio) && timeout) {
163 		iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
164 		dev_dbg(isp->dev, "waiting for ISPSSPM1 status bit to be 0x%x.\n",
165 			new_freq);
166 		udelay(100);
167 		timeout--;
168 	}
169 	if (timeout == 0) {
170 		dev_err(isp->dev, "DFS target freq is rejected by HW.\n");
171 		return -EINVAL;
172 	}
173 
174 	return 0;
175 }
176 
atomisp_freq_scaling(struct atomisp_device * isp,enum atomisp_dfs_mode mode,bool force)177 int atomisp_freq_scaling(struct atomisp_device *isp,
178 			 enum atomisp_dfs_mode mode,
179 			 bool force)
180 {
181 	const struct atomisp_dfs_config *dfs;
182 	unsigned int new_freq;
183 	struct atomisp_freq_scaling_rule curr_rules;
184 	int i, ret;
185 	unsigned short fps = 0;
186 
187 	dfs = isp->dfs;
188 
189 	if (dfs->lowest_freq == 0 || dfs->max_freq_at_vmin == 0 ||
190 	    dfs->highest_freq == 0 || dfs->dfs_table_size == 0 ||
191 	    !dfs->dfs_table) {
192 		dev_err(isp->dev, "DFS configuration is invalid.\n");
193 		return -EINVAL;
194 	}
195 
196 	if (mode == ATOMISP_DFS_MODE_LOW) {
197 		new_freq = dfs->lowest_freq;
198 		goto done;
199 	}
200 
201 	if (mode == ATOMISP_DFS_MODE_MAX) {
202 		new_freq = dfs->highest_freq;
203 		goto done;
204 	}
205 
206 	fps = atomisp_get_sensor_fps(&isp->asd);
207 	if (fps == 0) {
208 		dev_info(isp->dev,
209 			 "Sensor didn't report FPS. Using DFS max mode.\n");
210 		new_freq = dfs->highest_freq;
211 		goto done;
212 	}
213 
214 	curr_rules.width = isp->asd.fmt[ATOMISP_SUBDEV_PAD_SOURCE].fmt.width;
215 	curr_rules.height = isp->asd.fmt[ATOMISP_SUBDEV_PAD_SOURCE].fmt.height;
216 	curr_rules.fps = fps;
217 	curr_rules.run_mode = isp->asd.run_mode->val;
218 
219 	/* search for the target frequency by looping freq rules*/
220 	for (i = 0; i < dfs->dfs_table_size; i++) {
221 		if (curr_rules.width != dfs->dfs_table[i].width &&
222 		    dfs->dfs_table[i].width != ISP_FREQ_RULE_ANY)
223 			continue;
224 		if (curr_rules.height != dfs->dfs_table[i].height &&
225 		    dfs->dfs_table[i].height != ISP_FREQ_RULE_ANY)
226 			continue;
227 		if (curr_rules.fps != dfs->dfs_table[i].fps &&
228 		    dfs->dfs_table[i].fps != ISP_FREQ_RULE_ANY)
229 			continue;
230 		if (curr_rules.run_mode != dfs->dfs_table[i].run_mode &&
231 		    dfs->dfs_table[i].run_mode != ISP_FREQ_RULE_ANY)
232 			continue;
233 		break;
234 	}
235 
236 	if (i == dfs->dfs_table_size)
237 		new_freq = dfs->max_freq_at_vmin;
238 	else
239 		new_freq = dfs->dfs_table[i].isp_freq;
240 
241 done:
242 	dev_dbg(isp->dev, "DFS target frequency=%d.\n", new_freq);
243 
244 	if ((new_freq == isp->running_freq) && !force)
245 		return 0;
246 
247 	dev_dbg(isp->dev, "Programming DFS frequency to %d\n", new_freq);
248 
249 	ret = write_target_freq_to_hw(isp, new_freq);
250 	if (!ret) {
251 		isp->running_freq = new_freq;
252 		trace_ipu_pstate(new_freq, -1);
253 	}
254 	return ret;
255 }
256 
257 /*
258  * reset and restore ISP
259  */
atomisp_reset(struct atomisp_device * isp)260 int atomisp_reset(struct atomisp_device *isp)
261 {
262 	/* Reset ISP by power-cycling it */
263 	int ret = 0;
264 
265 	dev_dbg(isp->dev, "%s\n", __func__);
266 
267 	ret = atomisp_power_off(isp->dev);
268 	if (ret < 0)
269 		dev_err(isp->dev, "atomisp_power_off failed, %d\n", ret);
270 
271 	ret = atomisp_power_on(isp->dev);
272 	if (ret < 0) {
273 		dev_err(isp->dev, "atomisp_power_on failed, %d\n", ret);
274 		isp->isp_fatal_error = true;
275 	}
276 
277 	return ret;
278 }
279 
280 /*
281  * interrupt disable functions
282  */
disable_isp_irq(enum hrt_isp_css_irq irq)283 static void disable_isp_irq(enum hrt_isp_css_irq irq)
284 {
285 	irq_disable_channel(IRQ0_ID, irq);
286 
287 	if (irq != hrt_isp_css_irq_sp)
288 		return;
289 
290 	cnd_sp_irq_enable(SP0_ID, false);
291 }
292 
293 /*
294  * interrupt clean function
295  */
clear_isp_irq(enum hrt_isp_css_irq irq)296 static void clear_isp_irq(enum hrt_isp_css_irq irq)
297 {
298 	irq_clear_all(IRQ0_ID);
299 }
300 
atomisp_msi_irq_init(struct atomisp_device * isp)301 void atomisp_msi_irq_init(struct atomisp_device *isp)
302 {
303 	struct pci_dev *pdev = to_pci_dev(isp->dev);
304 	u32 msg32;
305 	u16 msg16;
306 
307 	pci_read_config_dword(pdev, PCI_MSI_CAPID, &msg32);
308 	msg32 |= 1 << MSI_ENABLE_BIT;
309 	pci_write_config_dword(pdev, PCI_MSI_CAPID, msg32);
310 
311 	msg32 = (1 << INTR_IER) | (1 << INTR_IIR);
312 	pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg32);
313 
314 	pci_read_config_word(pdev, PCI_COMMAND, &msg16);
315 	msg16 |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
316 		  PCI_COMMAND_INTX_DISABLE);
317 	pci_write_config_word(pdev, PCI_COMMAND, msg16);
318 }
319 
atomisp_msi_irq_uninit(struct atomisp_device * isp)320 void atomisp_msi_irq_uninit(struct atomisp_device *isp)
321 {
322 	struct pci_dev *pdev = to_pci_dev(isp->dev);
323 	u32 msg32;
324 	u16 msg16;
325 
326 	pci_read_config_dword(pdev, PCI_MSI_CAPID, &msg32);
327 	msg32 &=  ~(1 << MSI_ENABLE_BIT);
328 	pci_write_config_dword(pdev, PCI_MSI_CAPID, msg32);
329 
330 	msg32 = 0x0;
331 	pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg32);
332 
333 	pci_read_config_word(pdev, PCI_COMMAND, &msg16);
334 	msg16 &= ~(PCI_COMMAND_MASTER);
335 	pci_write_config_word(pdev, PCI_COMMAND, msg16);
336 }
337 
atomisp_sof_event(struct atomisp_sub_device * asd)338 static void atomisp_sof_event(struct atomisp_sub_device *asd)
339 {
340 	struct v4l2_event event = {0};
341 
342 	event.type = V4L2_EVENT_FRAME_SYNC;
343 	event.u.frame_sync.frame_sequence = atomic_read(&asd->sof_count);
344 
345 	v4l2_event_queue(asd->subdev.devnode, &event);
346 }
347 
atomisp_eof_event(struct atomisp_sub_device * asd,uint8_t exp_id)348 void atomisp_eof_event(struct atomisp_sub_device *asd, uint8_t exp_id)
349 {
350 	struct v4l2_event event = {0};
351 
352 	event.type = V4L2_EVENT_FRAME_END;
353 	event.u.frame_sync.frame_sequence = exp_id;
354 
355 	v4l2_event_queue(asd->subdev.devnode, &event);
356 }
357 
atomisp_3a_stats_ready_event(struct atomisp_sub_device * asd,uint8_t exp_id)358 static void atomisp_3a_stats_ready_event(struct atomisp_sub_device *asd,
359 	uint8_t exp_id)
360 {
361 	struct v4l2_event event = {0};
362 
363 	event.type = V4L2_EVENT_ATOMISP_3A_STATS_READY;
364 	event.u.frame_sync.frame_sequence = exp_id;
365 
366 	v4l2_event_queue(asd->subdev.devnode, &event);
367 }
368 
atomisp_metadata_ready_event(struct atomisp_sub_device * asd,enum atomisp_metadata_type md_type)369 static void atomisp_metadata_ready_event(struct atomisp_sub_device *asd,
370 	enum atomisp_metadata_type md_type)
371 {
372 	struct v4l2_event event = {0};
373 
374 	event.type = V4L2_EVENT_ATOMISP_METADATA_READY;
375 	event.u.data[0] = md_type;
376 
377 	v4l2_event_queue(asd->subdev.devnode, &event);
378 }
379 
atomisp_reset_event(struct atomisp_sub_device * asd)380 static void atomisp_reset_event(struct atomisp_sub_device *asd)
381 {
382 	struct v4l2_event event = {0};
383 
384 	event.type = V4L2_EVENT_ATOMISP_CSS_RESET;
385 
386 	v4l2_event_queue(asd->subdev.devnode, &event);
387 }
388 
print_csi_rx_errors(enum mipi_port_id port,struct atomisp_device * isp)389 static void print_csi_rx_errors(enum mipi_port_id port,
390 				struct atomisp_device *isp)
391 {
392 	u32 infos = 0;
393 
394 	atomisp_css_rx_get_irq_info(port, &infos);
395 
396 	dev_err(isp->dev, "CSI Receiver port %d errors:\n", port);
397 	if (infos & IA_CSS_RX_IRQ_INFO_BUFFER_OVERRUN)
398 		dev_err(isp->dev, "  buffer overrun");
399 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_SOT)
400 		dev_err(isp->dev, "  start-of-transmission error");
401 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_SOT_SYNC)
402 		dev_err(isp->dev, "  start-of-transmission sync error");
403 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_CONTROL)
404 		dev_err(isp->dev, "  control error");
405 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_ECC_DOUBLE)
406 		dev_err(isp->dev, "  2 or more ECC errors");
407 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_CRC)
408 		dev_err(isp->dev, "  CRC mismatch");
409 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_UNKNOWN_ID)
410 		dev_err(isp->dev, "  unknown error");
411 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_FRAME_SYNC)
412 		dev_err(isp->dev, "  frame sync error");
413 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_FRAME_DATA)
414 		dev_err(isp->dev, "  frame data error");
415 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_DATA_TIMEOUT)
416 		dev_err(isp->dev, "  data timeout");
417 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_UNKNOWN_ESC)
418 		dev_err(isp->dev, "  unknown escape command entry");
419 	if (infos & IA_CSS_RX_IRQ_INFO_ERR_LINE_SYNC)
420 		dev_err(isp->dev, "  line sync error");
421 }
422 
423 /* Clear irq reg */
clear_irq_reg(struct atomisp_device * isp)424 static void clear_irq_reg(struct atomisp_device *isp)
425 {
426 	struct pci_dev *pdev = to_pci_dev(isp->dev);
427 	u32 msg_ret;
428 
429 	pci_read_config_dword(pdev, PCI_INTERRUPT_CTRL, &msg_ret);
430 	msg_ret |= 1 << INTR_IIR;
431 	pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg_ret);
432 }
433 
434 /* interrupt handling function*/
atomisp_isr(int irq,void * dev)435 irqreturn_t atomisp_isr(int irq, void *dev)
436 {
437 	struct atomisp_device *isp = (struct atomisp_device *)dev;
438 	struct atomisp_css_event eof_event;
439 	unsigned int irq_infos = 0;
440 	unsigned long flags;
441 	int err;
442 
443 	spin_lock_irqsave(&isp->lock, flags);
444 
445 	if (!isp->css_initialized) {
446 		spin_unlock_irqrestore(&isp->lock, flags);
447 		return IRQ_HANDLED;
448 	}
449 	err = atomisp_css_irq_translate(isp, &irq_infos);
450 	if (err) {
451 		spin_unlock_irqrestore(&isp->lock, flags);
452 		return IRQ_NONE;
453 	}
454 
455 	clear_irq_reg(isp);
456 
457 	if (!isp->asd.streaming)
458 		goto out_nowake;
459 
460 	if (irq_infos & IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF) {
461 		atomic_inc(&isp->asd.sof_count);
462 		atomisp_sof_event(&isp->asd);
463 
464 		/*
465 		 * If sequence_temp and sequence are the same there where no frames
466 		 * lost so we can increase sequence_temp.
467 		 * If not then processing of frame is still in progress and driver
468 		 * needs to keep old sequence_temp value.
469 		 * NOTE: There is assumption here that ISP will not start processing
470 		 * next frame from sensor before old one is completely done.
471 		 */
472 		if (atomic_read(&isp->asd.sequence) == atomic_read(&isp->asd.sequence_temp))
473 			atomic_set(&isp->asd.sequence_temp, atomic_read(&isp->asd.sof_count));
474 
475 		dev_dbg_ratelimited(isp->dev, "irq:0x%x (SOF)\n", irq_infos);
476 		irq_infos &= ~IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF;
477 	}
478 
479 	if (irq_infos & IA_CSS_IRQ_INFO_EVENTS_READY)
480 		atomic_set(&isp->asd.sequence, atomic_read(&isp->asd.sequence_temp));
481 
482 	if ((irq_infos & IA_CSS_IRQ_INFO_INPUT_SYSTEM_ERROR) ||
483 	    (irq_infos & IA_CSS_IRQ_INFO_IF_ERROR)) {
484 		/* handle mipi receiver error */
485 		u32 rx_infos;
486 		enum mipi_port_id port;
487 
488 		for (port = MIPI_PORT0_ID; port <= MIPI_PORT2_ID;
489 		     port++) {
490 			print_csi_rx_errors(port, isp);
491 			atomisp_css_rx_get_irq_info(port, &rx_infos);
492 			atomisp_css_rx_clear_irq_info(port, rx_infos);
493 		}
494 	}
495 
496 	if (irq_infos & IA_CSS_IRQ_INFO_ISYS_EVENTS_READY) {
497 		while (ia_css_dequeue_isys_event(&eof_event.event) == 0) {
498 			atomisp_eof_event(&isp->asd, eof_event.event.exp_id);
499 			dev_dbg_ratelimited(isp->dev, "ISYS event: EOF exp_id %d\n",
500 					    eof_event.event.exp_id);
501 		}
502 
503 		irq_infos &= ~IA_CSS_IRQ_INFO_ISYS_EVENTS_READY;
504 		if (irq_infos == 0)
505 			goto out_nowake;
506 	}
507 
508 	spin_unlock_irqrestore(&isp->lock, flags);
509 
510 	dev_dbg_ratelimited(isp->dev, "irq:0x%x (unhandled)\n", irq_infos);
511 
512 	return IRQ_WAKE_THREAD;
513 
514 out_nowake:
515 	spin_unlock_irqrestore(&isp->lock, flags);
516 
517 	if (irq_infos)
518 		dev_dbg_ratelimited(isp->dev, "irq:0x%x (ignored, as not streaming anymore)\n",
519 				    irq_infos);
520 
521 	return IRQ_HANDLED;
522 }
523 
atomisp_clear_css_buffer_counters(struct atomisp_sub_device * asd)524 void atomisp_clear_css_buffer_counters(struct atomisp_sub_device *asd)
525 {
526 	int i;
527 
528 	memset(asd->s3a_bufs_in_css, 0, sizeof(asd->s3a_bufs_in_css));
529 	for (i = 0; i < ATOMISP_INPUT_STREAM_NUM; i++)
530 		memset(asd->metadata_bufs_in_css[i], 0,
531 		       sizeof(asd->metadata_bufs_in_css[i]));
532 	asd->dis_bufs_in_css = 0;
533 }
534 
535 /* 0x100000 is the start of dmem inside SP */
536 #define SP_DMEM_BASE	0x100000
537 
dump_sp_dmem(struct atomisp_device * isp,unsigned int addr,unsigned int size)538 void dump_sp_dmem(struct atomisp_device *isp, unsigned int addr,
539 		  unsigned int size)
540 {
541 	unsigned int data = 0;
542 	unsigned int size32 = DIV_ROUND_UP(size, sizeof(u32));
543 
544 	dev_dbg(isp->dev, "atomisp mmio base: %p\n", isp->base);
545 	dev_dbg(isp->dev, "%s, addr:0x%x, size: %d, size32: %d\n", __func__,
546 		addr, size, size32);
547 	if (size32 * 4 + addr > 0x4000) {
548 		dev_err(isp->dev, "illegal size (%d) or addr (0x%x)\n",
549 			size32, addr);
550 		return;
551 	}
552 	addr += SP_DMEM_BASE;
553 	addr &= 0x003FFFFF;
554 	do {
555 		data = readl(isp->base + addr);
556 		dev_dbg(isp->dev, "%s, \t [0x%x]:0x%x\n", __func__, addr, data);
557 		addr += sizeof(u32);
558 	} while (--size32);
559 }
560 
atomisp_buffers_in_css(struct atomisp_video_pipe * pipe)561 int atomisp_buffers_in_css(struct atomisp_video_pipe *pipe)
562 {
563 	unsigned long irqflags;
564 	struct list_head *pos;
565 	int buffers_in_css = 0;
566 
567 	spin_lock_irqsave(&pipe->irq_lock, irqflags);
568 
569 	list_for_each(pos, &pipe->buffers_in_css)
570 		buffers_in_css++;
571 
572 	spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
573 
574 	return buffers_in_css;
575 }
576 
atomisp_buffer_done(struct ia_css_frame * frame,enum vb2_buffer_state state)577 void atomisp_buffer_done(struct ia_css_frame *frame, enum vb2_buffer_state state)
578 {
579 	struct atomisp_video_pipe *pipe = vb_to_pipe(&frame->vb.vb2_buf);
580 
581 	lockdep_assert_held(&pipe->irq_lock);
582 
583 	frame->vb.vb2_buf.timestamp = ktime_get_ns();
584 	frame->vb.field = pipe->pix.field;
585 	frame->vb.sequence = atomic_read(&pipe->asd->sequence);
586 	list_del(&frame->queue);
587 	if (state == VB2_BUF_STATE_DONE)
588 		vb2_set_plane_payload(&frame->vb.vb2_buf, 0, pipe->pix.sizeimage);
589 	vb2_buffer_done(&frame->vb.vb2_buf, state);
590 }
591 
atomisp_flush_video_pipe(struct atomisp_video_pipe * pipe,enum vb2_buffer_state state,bool warn_on_css_frames)592 void atomisp_flush_video_pipe(struct atomisp_video_pipe *pipe, enum vb2_buffer_state state,
593 			      bool warn_on_css_frames)
594 {
595 	struct ia_css_frame *frame, *_frame;
596 	unsigned long irqflags;
597 
598 	spin_lock_irqsave(&pipe->irq_lock, irqflags);
599 
600 	list_for_each_entry_safe(frame, _frame, &pipe->buffers_in_css, queue) {
601 		if (warn_on_css_frames)
602 			dev_warn(pipe->isp->dev, "Warning: CSS frames queued on flush\n");
603 		atomisp_buffer_done(frame, state);
604 	}
605 
606 	list_for_each_entry_safe(frame, _frame, &pipe->activeq, queue)
607 		atomisp_buffer_done(frame, state);
608 
609 	list_for_each_entry_safe(frame, _frame, &pipe->buffers_waiting_for_param, queue) {
610 		pipe->frame_request_config_id[frame->vb.vb2_buf.index] = 0;
611 		atomisp_buffer_done(frame, state);
612 	}
613 
614 	spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
615 }
616 
617 /* clean out the parameters that did not apply */
atomisp_flush_params_queue(struct atomisp_video_pipe * pipe)618 void atomisp_flush_params_queue(struct atomisp_video_pipe *pipe)
619 {
620 	struct atomisp_css_params_with_list *param;
621 
622 	while (!list_empty(&pipe->per_frame_params)) {
623 		param = list_entry(pipe->per_frame_params.next,
624 				   struct atomisp_css_params_with_list, list);
625 		list_del(&param->list);
626 		atomisp_free_css_parameters(&param->params);
627 		kvfree(param);
628 	}
629 }
630 
631 /* Re-queue per-frame parameters */
atomisp_recover_params_queue(struct atomisp_video_pipe * pipe)632 static void atomisp_recover_params_queue(struct atomisp_video_pipe *pipe)
633 {
634 	struct atomisp_css_params_with_list *param;
635 	int i;
636 
637 	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
638 		param = pipe->frame_params[i];
639 		if (param)
640 			list_add_tail(&param->list, &pipe->per_frame_params);
641 		pipe->frame_params[i] = NULL;
642 	}
643 	atomisp_handle_parameter_and_buffer(pipe);
644 }
645 
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)646 void atomisp_buf_done(struct atomisp_sub_device *asd, int error,
647 		      enum ia_css_buffer_type buf_type,
648 		      enum ia_css_pipe_id css_pipe_id,
649 		      bool q_buffers, enum atomisp_input_stream_id stream_id)
650 {
651 	struct atomisp_video_pipe *pipe = NULL;
652 	struct atomisp_css_buffer buffer;
653 	bool requeue = false;
654 	unsigned long irqflags;
655 	struct ia_css_frame *frame = NULL;
656 	struct atomisp_s3a_buf *s3a_buf = NULL, *_s3a_buf_tmp, *s3a_iter;
657 	struct atomisp_dis_buf *dis_buf = NULL, *_dis_buf_tmp, *dis_iter;
658 	struct atomisp_metadata_buf *md_buf = NULL, *_md_buf_tmp, *md_iter;
659 	enum atomisp_metadata_type md_type;
660 	struct atomisp_device *isp = asd->isp;
661 	int i, err;
662 
663 	lockdep_assert_held(&isp->mutex);
664 
665 	if (
666 	    buf_type != IA_CSS_BUFFER_TYPE_METADATA &&
667 	    buf_type != IA_CSS_BUFFER_TYPE_3A_STATISTICS &&
668 	    buf_type != IA_CSS_BUFFER_TYPE_DIS_STATISTICS &&
669 	    buf_type != IA_CSS_BUFFER_TYPE_OUTPUT_FRAME &&
670 	    buf_type != IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME &&
671 	    buf_type != IA_CSS_BUFFER_TYPE_RAW_OUTPUT_FRAME &&
672 	    buf_type != IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME &&
673 	    buf_type != IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME) {
674 		dev_err(isp->dev, "%s, unsupported buffer type: %d\n",
675 			__func__, buf_type);
676 		return;
677 	}
678 
679 	memset(&buffer, 0, sizeof(struct atomisp_css_buffer));
680 	buffer.css_buffer.type = buf_type;
681 	err = atomisp_css_dequeue_buffer(asd, stream_id, css_pipe_id,
682 					 buf_type, &buffer);
683 	if (err) {
684 		dev_err(isp->dev,
685 			"atomisp_css_dequeue_buffer failed: 0x%x\n", err);
686 		return;
687 	}
688 
689 	switch (buf_type) {
690 	case IA_CSS_BUFFER_TYPE_3A_STATISTICS:
691 		list_for_each_entry_safe(s3a_iter, _s3a_buf_tmp,
692 					 &asd->s3a_stats_in_css, list) {
693 			if (s3a_iter->s3a_data ==
694 			    buffer.css_buffer.data.stats_3a) {
695 				list_del_init(&s3a_iter->list);
696 				list_add_tail(&s3a_iter->list,
697 					      &asd->s3a_stats_ready);
698 				s3a_buf = s3a_iter;
699 				break;
700 			}
701 		}
702 
703 		asd->s3a_bufs_in_css[css_pipe_id]--;
704 		atomisp_3a_stats_ready_event(asd, buffer.css_buffer.exp_id);
705 		if (s3a_buf)
706 			dev_dbg(isp->dev, "%s: s3a stat with exp_id %d is ready\n",
707 				__func__, s3a_buf->s3a_data->exp_id);
708 		else
709 			dev_dbg(isp->dev, "%s: s3a stat is ready with no exp_id found\n",
710 				__func__);
711 		break;
712 	case IA_CSS_BUFFER_TYPE_METADATA:
713 		if (error)
714 			break;
715 
716 		md_type = ATOMISP_MAIN_METADATA;
717 		list_for_each_entry_safe(md_iter, _md_buf_tmp,
718 					 &asd->metadata_in_css[md_type], list) {
719 			if (md_iter->metadata ==
720 			    buffer.css_buffer.data.metadata) {
721 				list_del_init(&md_iter->list);
722 				list_add_tail(&md_iter->list,
723 					      &asd->metadata_ready[md_type]);
724 				md_buf = md_iter;
725 				break;
726 			}
727 		}
728 		asd->metadata_bufs_in_css[stream_id][css_pipe_id]--;
729 		atomisp_metadata_ready_event(asd, md_type);
730 		if (md_buf)
731 			dev_dbg(isp->dev, "%s: metadata with exp_id %d is ready\n",
732 				__func__, md_buf->metadata->exp_id);
733 		else
734 			dev_dbg(isp->dev, "%s: metadata is ready with no exp_id found\n",
735 				__func__);
736 		break;
737 	case IA_CSS_BUFFER_TYPE_DIS_STATISTICS:
738 		list_for_each_entry_safe(dis_iter, _dis_buf_tmp,
739 					 &asd->dis_stats_in_css, list) {
740 			if (dis_iter->dis_data ==
741 			    buffer.css_buffer.data.stats_dvs) {
742 				spin_lock_irqsave(&asd->dis_stats_lock,
743 						  irqflags);
744 				list_del_init(&dis_iter->list);
745 				list_add(&dis_iter->list, &asd->dis_stats);
746 				asd->params.dis_proj_data_valid = true;
747 				spin_unlock_irqrestore(&asd->dis_stats_lock,
748 						       irqflags);
749 				dis_buf = dis_iter;
750 				break;
751 			}
752 		}
753 		asd->dis_bufs_in_css--;
754 		if (dis_buf)
755 			dev_dbg(isp->dev, "%s: dis stat with exp_id %d is ready\n",
756 				__func__, dis_buf->dis_data->exp_id);
757 		else
758 			dev_dbg(isp->dev, "%s: dis stat is ready with no exp_id found\n",
759 				__func__);
760 		break;
761 	case IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME:
762 	case IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME:
763 		frame = buffer.css_buffer.data.frame;
764 		if (!frame) {
765 			WARN_ON(1);
766 			break;
767 		}
768 		if (!frame->valid)
769 			error = true;
770 
771 		pipe = vb_to_pipe(&frame->vb.vb2_buf);
772 
773 		dev_dbg(isp->dev, "%s: vf frame with exp_id %d is ready\n",
774 			__func__, frame->exp_id);
775 		pipe->frame_config_id[frame->vb.vb2_buf.index] = frame->isp_config_id;
776 		break;
777 	case IA_CSS_BUFFER_TYPE_OUTPUT_FRAME:
778 	case IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
779 		frame = buffer.css_buffer.data.frame;
780 		if (!frame) {
781 			WARN_ON(1);
782 			break;
783 		}
784 
785 		if (!frame->valid)
786 			error = true;
787 
788 		pipe = vb_to_pipe(&frame->vb.vb2_buf);
789 
790 		dev_dbg(isp->dev, "%s: main frame with exp_id %d is ready\n",
791 			__func__, frame->exp_id);
792 
793 		i = frame->vb.vb2_buf.index;
794 
795 		/* free the parameters */
796 		if (pipe->frame_params[i]) {
797 			if (asd->params.dvs_6axis == pipe->frame_params[i]->params.dvs_6axis)
798 				asd->params.dvs_6axis = NULL;
799 			atomisp_free_css_parameters(&pipe->frame_params[i]->params);
800 			kvfree(pipe->frame_params[i]);
801 			pipe->frame_params[i] = NULL;
802 		}
803 
804 		pipe->frame_config_id[i] = frame->isp_config_id;
805 
806 		if (asd->params.css_update_params_needed) {
807 			atomisp_apply_css_parameters(asd,
808 						     &asd->params.css_param);
809 			if (asd->params.css_param.update_flag.dz_config)
810 				asd->params.config.dz_config = &asd->params.css_param.dz_config;
811 			/* New global dvs 6axis config should be blocked
812 			 * here if there's a buffer with per-frame parameters
813 			 * pending in CSS frame buffer queue.
814 			 * This is to aviod zooming vibration since global
815 			 * parameters take effect immediately while
816 			 * per-frame parameters are taken after previous
817 			 * buffers in CSS got processed.
818 			 */
819 			if (asd->params.dvs_6axis)
820 				atomisp_css_set_dvs_6axis(asd,
821 							  asd->params.dvs_6axis);
822 			else
823 				asd->params.css_update_params_needed = false;
824 			/* The update flag should not be cleaned here
825 			 * since it is still going to be used to make up
826 			 * following per-frame parameters.
827 			 * This will introduce more copy work since each
828 			 * time when updating global parameters, the whole
829 			 * parameter set are applied.
830 			 * FIXME: A new set of parameter copy functions can
831 			 * be added to make up per-frame parameters based on
832 			 * solid structures stored in asd->params.css_param
833 			 * instead of using shadow pointers in update flag.
834 			 */
835 			atomisp_css_update_isp_params(asd);
836 		}
837 		break;
838 	default:
839 		break;
840 	}
841 	if (frame) {
842 		spin_lock_irqsave(&pipe->irq_lock, irqflags);
843 		atomisp_buffer_done(frame, error ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
844 		spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
845 	}
846 
847 	/*
848 	 * Requeue should only be done for 3a and dis buffers.
849 	 * Queue/dequeue order will change if driver recycles image buffers.
850 	 */
851 	if (requeue) {
852 		err = atomisp_css_queue_buffer(asd,
853 					       stream_id, css_pipe_id,
854 					       buf_type, &buffer);
855 		if (err)
856 			dev_err(isp->dev, "%s, q to css fails: %d\n",
857 				__func__, err);
858 		return;
859 	}
860 	if (!error && q_buffers)
861 		atomisp_qbuffers_to_css(asd);
862 }
863 
atomisp_assert_recovery_work(struct work_struct * work)864 void atomisp_assert_recovery_work(struct work_struct *work)
865 {
866 	struct atomisp_device *isp = container_of(work, struct atomisp_device,
867 						  assert_recovery_work);
868 	struct pci_dev *pdev = to_pci_dev(isp->dev);
869 	unsigned long flags;
870 	int ret;
871 
872 	mutex_lock(&isp->mutex);
873 
874 	if (!isp->asd.streaming)
875 		goto out_unlock;
876 
877 	atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF, false);
878 
879 	spin_lock_irqsave(&isp->lock, flags);
880 	isp->asd.streaming = false;
881 	spin_unlock_irqrestore(&isp->lock, flags);
882 
883 	/* stream off sensor */
884 	ret = v4l2_subdev_call(isp->inputs[isp->asd.input_curr].camera, video, s_stream, 0);
885 	if (ret)
886 		dev_warn(isp->dev, "Stopping sensor stream failed: %d\n", ret);
887 
888 	atomisp_clear_css_buffer_counters(&isp->asd);
889 
890 	atomisp_css_stop(&isp->asd, true);
891 
892 	isp->asd.preview_exp_id = 1;
893 	isp->asd.postview_exp_id = 1;
894 	/* notify HAL the CSS reset */
895 	dev_dbg(isp->dev, "send reset event to %s\n", isp->asd.subdev.devnode->name);
896 	atomisp_reset_event(&isp->asd);
897 
898 	/* clear irq */
899 	disable_isp_irq(hrt_isp_css_irq_sp);
900 	clear_isp_irq(hrt_isp_css_irq_sp);
901 
902 	/* Set the SRSE to 3 before resetting */
903 	pci_write_config_dword(pdev, PCI_I_CONTROL,
904 			       isp->saved_regs.i_control | MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK);
905 
906 	/* reset ISP and restore its state */
907 	atomisp_reset(isp);
908 
909 	atomisp_css_input_set_mode(&isp->asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
910 
911 	/* Recreate streams destroyed by atomisp_css_stop() */
912 	atomisp_create_pipes_stream(&isp->asd);
913 
914 	/* Invalidate caches. FIXME: should flush only necessary buffers */
915 	wbinvd();
916 
917 	if (atomisp_css_start(&isp->asd)) {
918 		dev_warn(isp->dev, "start SP failed, so do not set streaming to be enable!\n");
919 	} else {
920 		spin_lock_irqsave(&isp->lock, flags);
921 		isp->asd.streaming = true;
922 		spin_unlock_irqrestore(&isp->lock, flags);
923 	}
924 
925 	atomisp_csi2_configure(&isp->asd);
926 
927 	atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF,
928 			       atomisp_css_valid_sof(isp));
929 
930 	if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, true) < 0)
931 		dev_dbg(isp->dev, "DFS auto failed while recovering!\n");
932 
933 	/* Dequeueing buffers is not needed, CSS will recycle buffers that it has */
934 	atomisp_flush_video_pipe(&isp->asd.video_out, VB2_BUF_STATE_ERROR, false);
935 
936 	/* Requeue unprocessed per-frame parameters. */
937 	atomisp_recover_params_queue(&isp->asd.video_out);
938 
939 	ret = v4l2_subdev_call(isp->inputs[isp->asd.input_curr].camera, video, s_stream, 1);
940 	if (ret)
941 		dev_err(isp->dev, "Starting sensor stream failed: %d\n", ret);
942 
943 out_unlock:
944 	mutex_unlock(&isp->mutex);
945 }
946 
atomisp_isr_thread(int irq,void * isp_ptr)947 irqreturn_t atomisp_isr_thread(int irq, void *isp_ptr)
948 {
949 	struct atomisp_device *isp = isp_ptr;
950 	unsigned long flags;
951 	bool streaming;
952 
953 	spin_lock_irqsave(&isp->lock, flags);
954 	streaming = isp->asd.streaming;
955 	spin_unlock_irqrestore(&isp->lock, flags);
956 
957 	if (!streaming)
958 		return IRQ_HANDLED;
959 
960 	/*
961 	 * The standard CSS2.0 API tells the following calling sequence of
962 	 * dequeue ready buffers:
963 	 * while (ia_css_dequeue_psys_event(...)) {
964 	 *	switch (event.type) {
965 	 *	...
966 	 *	ia_css_pipe_dequeue_buffer()
967 	 *	}
968 	 * }
969 	 * That is, dequeue event and buffer are one after another.
970 	 *
971 	 * But the following implementation is to first deuque all the event
972 	 * to a FIFO, then process the event in the FIFO.
973 	 * This will not have issue in single stream mode, but it do have some
974 	 * issue in multiple stream case. The issue is that
975 	 * ia_css_pipe_dequeue_buffer() will not return the corrent buffer in
976 	 * a specific pipe.
977 	 *
978 	 * This is due to ia_css_pipe_dequeue_buffer() does not take the
979 	 * ia_css_pipe parameter.
980 	 *
981 	 * So:
982 	 * For CSS2.0: we change the way to not dequeue all the event at one
983 	 * time, instead, dequue one and process one, then another
984 	 */
985 	mutex_lock(&isp->mutex);
986 	atomisp_css_isr_thread(isp);
987 	mutex_unlock(&isp->mutex);
988 
989 	return IRQ_HANDLED;
990 }
991 
992 /*
993  * Get internal fmt according to V4L2 fmt
994  */
995 static enum ia_css_frame_format
v4l2_fmt_to_sh_fmt(u32 fmt)996 v4l2_fmt_to_sh_fmt(u32 fmt)
997 {
998 	switch (fmt) {
999 	case V4L2_PIX_FMT_YUV420:
1000 				return IA_CSS_FRAME_FORMAT_YUV420;
1001 	case V4L2_PIX_FMT_YVU420:
1002 		return IA_CSS_FRAME_FORMAT_YV12;
1003 	case V4L2_PIX_FMT_YUV422P:
1004 		return IA_CSS_FRAME_FORMAT_YUV422;
1005 	case V4L2_PIX_FMT_YUV444:
1006 		return IA_CSS_FRAME_FORMAT_YUV444;
1007 	case V4L2_PIX_FMT_NV12:
1008 		return IA_CSS_FRAME_FORMAT_NV12;
1009 	case V4L2_PIX_FMT_NV21:
1010 		return IA_CSS_FRAME_FORMAT_NV21;
1011 	case V4L2_PIX_FMT_NV16:
1012 		return IA_CSS_FRAME_FORMAT_NV16;
1013 	case V4L2_PIX_FMT_NV61:
1014 		return IA_CSS_FRAME_FORMAT_NV61;
1015 	case V4L2_PIX_FMT_UYVY:
1016 		return IA_CSS_FRAME_FORMAT_UYVY;
1017 	case V4L2_PIX_FMT_YUYV:
1018 		return IA_CSS_FRAME_FORMAT_YUYV;
1019 	case V4L2_PIX_FMT_RGB24:
1020 		return IA_CSS_FRAME_FORMAT_PLANAR_RGB888;
1021 	case V4L2_PIX_FMT_RGBX32:
1022 		return IA_CSS_FRAME_FORMAT_RGBA888;
1023 	case V4L2_PIX_FMT_RGB565:
1024 		return IA_CSS_FRAME_FORMAT_RGB565;
1025 #if 0
1026 	case V4L2_PIX_FMT_JPEG:
1027 	case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
1028 		return IA_CSS_FRAME_FORMAT_BINARY_8;
1029 #endif
1030 	case V4L2_PIX_FMT_SBGGR16:
1031 	case V4L2_PIX_FMT_SBGGR10:
1032 	case V4L2_PIX_FMT_SGBRG10:
1033 	case V4L2_PIX_FMT_SGRBG10:
1034 	case V4L2_PIX_FMT_SRGGB10:
1035 	case V4L2_PIX_FMT_SBGGR12:
1036 	case V4L2_PIX_FMT_SGBRG12:
1037 	case V4L2_PIX_FMT_SGRBG12:
1038 	case V4L2_PIX_FMT_SRGGB12:
1039 	case V4L2_PIX_FMT_SBGGR8:
1040 	case V4L2_PIX_FMT_SGBRG8:
1041 	case V4L2_PIX_FMT_SGRBG8:
1042 	case V4L2_PIX_FMT_SRGGB8:
1043 		return IA_CSS_FRAME_FORMAT_RAW;
1044 	default:
1045 		return -EINVAL;
1046 	}
1047 }
1048 
1049 /*
1050  * raw format match between SH format and V4L2 format
1051  */
raw_output_format_match_input(u32 input,u32 output)1052 static int raw_output_format_match_input(u32 input, u32 output)
1053 {
1054 	if ((input == ATOMISP_INPUT_FORMAT_RAW_12) &&
1055 	    ((output == V4L2_PIX_FMT_SRGGB12) ||
1056 	     (output == V4L2_PIX_FMT_SGRBG12) ||
1057 	     (output == V4L2_PIX_FMT_SBGGR12) ||
1058 	     (output == V4L2_PIX_FMT_SGBRG12)))
1059 		return 0;
1060 
1061 	if ((input == ATOMISP_INPUT_FORMAT_RAW_10) &&
1062 	    ((output == V4L2_PIX_FMT_SRGGB10) ||
1063 	     (output == V4L2_PIX_FMT_SGRBG10) ||
1064 	     (output == V4L2_PIX_FMT_SBGGR10) ||
1065 	     (output == V4L2_PIX_FMT_SGBRG10)))
1066 		return 0;
1067 
1068 	if ((input == ATOMISP_INPUT_FORMAT_RAW_8) &&
1069 	    ((output == V4L2_PIX_FMT_SRGGB8) ||
1070 	     (output == V4L2_PIX_FMT_SGRBG8) ||
1071 	     (output == V4L2_PIX_FMT_SBGGR8) ||
1072 	     (output == V4L2_PIX_FMT_SGBRG8)))
1073 		return 0;
1074 
1075 	if ((input == ATOMISP_INPUT_FORMAT_RAW_16) && (output == V4L2_PIX_FMT_SBGGR16))
1076 		return 0;
1077 
1078 	return -EINVAL;
1079 }
1080 
atomisp_get_pixel_depth(u32 pixelformat)1081 u32 atomisp_get_pixel_depth(u32 pixelformat)
1082 {
1083 	switch (pixelformat) {
1084 	case V4L2_PIX_FMT_YUV420:
1085 	case V4L2_PIX_FMT_NV12:
1086 	case V4L2_PIX_FMT_NV21:
1087 	case V4L2_PIX_FMT_YVU420:
1088 		return 12;
1089 	case V4L2_PIX_FMT_YUV422P:
1090 	case V4L2_PIX_FMT_YUYV:
1091 	case V4L2_PIX_FMT_UYVY:
1092 	case V4L2_PIX_FMT_NV16:
1093 	case V4L2_PIX_FMT_NV61:
1094 	case V4L2_PIX_FMT_RGB565:
1095 	case V4L2_PIX_FMT_SBGGR16:
1096 	case V4L2_PIX_FMT_SBGGR12:
1097 	case V4L2_PIX_FMT_SGBRG12:
1098 	case V4L2_PIX_FMT_SGRBG12:
1099 	case V4L2_PIX_FMT_SRGGB12:
1100 	case V4L2_PIX_FMT_SBGGR10:
1101 	case V4L2_PIX_FMT_SGBRG10:
1102 	case V4L2_PIX_FMT_SGRBG10:
1103 	case V4L2_PIX_FMT_SRGGB10:
1104 		return 16;
1105 	case V4L2_PIX_FMT_RGB24:
1106 	case V4L2_PIX_FMT_YUV444:
1107 		return 24;
1108 	case V4L2_PIX_FMT_RGBX32:
1109 		return 32;
1110 	case V4L2_PIX_FMT_JPEG:
1111 	case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
1112 	case V4L2_PIX_FMT_SBGGR8:
1113 	case V4L2_PIX_FMT_SGBRG8:
1114 	case V4L2_PIX_FMT_SGRBG8:
1115 	case V4L2_PIX_FMT_SRGGB8:
1116 		return 8;
1117 	default:
1118 		return 8 * 2;	/* raw type now */
1119 	}
1120 }
1121 
atomisp_is_mbuscode_raw(uint32_t code)1122 bool atomisp_is_mbuscode_raw(uint32_t code)
1123 {
1124 	return code >= 0x3000 && code < 0x4000;
1125 }
1126 
1127 /*
1128  * ISP features control function
1129  */
1130 
1131 /*
1132  * Set ISP capture mode based on current settings
1133  */
atomisp_update_capture_mode(struct atomisp_sub_device * asd)1134 static void atomisp_update_capture_mode(struct atomisp_sub_device *asd)
1135 {
1136 	if (asd->params.gdc_cac_en)
1137 		atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_ADVANCED);
1138 	else if (asd->params.low_light)
1139 		atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_LOW_LIGHT);
1140 	else if (asd->video_out.sh_fmt == IA_CSS_FRAME_FORMAT_RAW)
1141 		atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
1142 	else
1143 		atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_PRIMARY);
1144 }
1145 
1146 /*
1147  * Function to enable/disable lens geometry distortion correction (GDC) and
1148  * chromatic aberration correction (CAC)
1149  */
atomisp_gdc_cac(struct atomisp_sub_device * asd,int flag,__s32 * value)1150 int atomisp_gdc_cac(struct atomisp_sub_device *asd, int flag,
1151 		    __s32 *value)
1152 {
1153 	if (flag == 0) {
1154 		*value = asd->params.gdc_cac_en;
1155 		return 0;
1156 	}
1157 
1158 	asd->params.gdc_cac_en = !!*value;
1159 	if (asd->params.gdc_cac_en)
1160 		asd->params.config.morph_table = asd->params.css_param.morph_table;
1161 	else
1162 		asd->params.config.morph_table = NULL;
1163 
1164 	asd->params.css_update_params_needed = true;
1165 	atomisp_update_capture_mode(asd);
1166 	return 0;
1167 }
1168 
1169 /*
1170  * Function to enable/disable low light mode including ANR
1171  */
atomisp_low_light(struct atomisp_sub_device * asd,int flag,__s32 * value)1172 int atomisp_low_light(struct atomisp_sub_device *asd, int flag,
1173 		      __s32 *value)
1174 {
1175 	if (flag == 0) {
1176 		*value = asd->params.low_light;
1177 		return 0;
1178 	}
1179 
1180 	asd->params.low_light = (*value != 0);
1181 	atomisp_update_capture_mode(asd);
1182 	return 0;
1183 }
1184 
1185 /*
1186  * Function to enable/disable extra noise reduction (XNR) in low light
1187  * condition
1188  */
atomisp_xnr(struct atomisp_sub_device * asd,int flag,int * xnr_enable)1189 int atomisp_xnr(struct atomisp_sub_device *asd, int flag,
1190 		int *xnr_enable)
1191 {
1192 	if (flag == 0) {
1193 		*xnr_enable = asd->params.xnr_en;
1194 		return 0;
1195 	}
1196 
1197 	atomisp_css_capture_enable_xnr(asd, !!*xnr_enable);
1198 
1199 	return 0;
1200 }
1201 
1202 /*
1203  * Function to configure bayer noise reduction
1204  */
atomisp_nr(struct atomisp_sub_device * asd,int flag,struct atomisp_nr_config * arg)1205 int atomisp_nr(struct atomisp_sub_device *asd, int flag,
1206 	       struct atomisp_nr_config *arg)
1207 {
1208 	if (flag == 0) {
1209 		/* Get nr config from current setup */
1210 		if (atomisp_css_get_nr_config(asd, arg))
1211 			return -EINVAL;
1212 	} else {
1213 		/* Set nr config to isp parameters */
1214 		memcpy(&asd->params.css_param.nr_config, arg,
1215 		       sizeof(struct ia_css_nr_config));
1216 		asd->params.config.nr_config = &asd->params.css_param.nr_config;
1217 		asd->params.css_update_params_needed = true;
1218 	}
1219 	return 0;
1220 }
1221 
1222 /*
1223  * Function to configure temporal noise reduction (TNR)
1224  */
atomisp_tnr(struct atomisp_sub_device * asd,int flag,struct atomisp_tnr_config * config)1225 int atomisp_tnr(struct atomisp_sub_device *asd, int flag,
1226 		struct atomisp_tnr_config *config)
1227 {
1228 	/* Get tnr config from current setup */
1229 	if (flag == 0) {
1230 		/* Get tnr config from current setup */
1231 		if (atomisp_css_get_tnr_config(asd, config))
1232 			return -EINVAL;
1233 	} else {
1234 		/* Set tnr config to isp parameters */
1235 		memcpy(&asd->params.css_param.tnr_config, config,
1236 		       sizeof(struct ia_css_tnr_config));
1237 		asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
1238 		asd->params.css_update_params_needed = true;
1239 	}
1240 
1241 	return 0;
1242 }
1243 
1244 /*
1245  * Function to configure black level compensation
1246  */
atomisp_black_level(struct atomisp_sub_device * asd,int flag,struct atomisp_ob_config * config)1247 int atomisp_black_level(struct atomisp_sub_device *asd, int flag,
1248 			struct atomisp_ob_config *config)
1249 {
1250 	if (flag == 0) {
1251 		/* Get ob config from current setup */
1252 		if (atomisp_css_get_ob_config(asd, config))
1253 			return -EINVAL;
1254 	} else {
1255 		/* Set ob config to isp parameters */
1256 		memcpy(&asd->params.css_param.ob_config, config,
1257 		       sizeof(struct ia_css_ob_config));
1258 		asd->params.config.ob_config = &asd->params.css_param.ob_config;
1259 		asd->params.css_update_params_needed = true;
1260 	}
1261 
1262 	return 0;
1263 }
1264 
1265 /*
1266  * Function to configure edge enhancement
1267  */
atomisp_ee(struct atomisp_sub_device * asd,int flag,struct atomisp_ee_config * config)1268 int atomisp_ee(struct atomisp_sub_device *asd, int flag,
1269 	       struct atomisp_ee_config *config)
1270 {
1271 	if (flag == 0) {
1272 		/* Get ee config from current setup */
1273 		if (atomisp_css_get_ee_config(asd, config))
1274 			return -EINVAL;
1275 	} else {
1276 		/* Set ee config to isp parameters */
1277 		memcpy(&asd->params.css_param.ee_config, config,
1278 		       sizeof(asd->params.css_param.ee_config));
1279 		asd->params.config.ee_config = &asd->params.css_param.ee_config;
1280 		asd->params.css_update_params_needed = true;
1281 	}
1282 
1283 	return 0;
1284 }
1285 
1286 /*
1287  * Function to update Gamma table for gamma, brightness and contrast config
1288  */
atomisp_gamma(struct atomisp_sub_device * asd,int flag,struct atomisp_gamma_table * config)1289 int atomisp_gamma(struct atomisp_sub_device *asd, int flag,
1290 		  struct atomisp_gamma_table *config)
1291 {
1292 	if (flag == 0) {
1293 		/* Get gamma table from current setup */
1294 		if (atomisp_css_get_gamma_table(asd, config))
1295 			return -EINVAL;
1296 	} else {
1297 		/* Set gamma table to isp parameters */
1298 		memcpy(&asd->params.css_param.gamma_table, config,
1299 		       sizeof(asd->params.css_param.gamma_table));
1300 		asd->params.config.gamma_table = &asd->params.css_param.gamma_table;
1301 	}
1302 
1303 	return 0;
1304 }
1305 
1306 /*
1307  * Function to update Ctc table for Chroma Enhancement
1308  */
atomisp_ctc(struct atomisp_sub_device * asd,int flag,struct atomisp_ctc_table * config)1309 int atomisp_ctc(struct atomisp_sub_device *asd, int flag,
1310 		struct atomisp_ctc_table *config)
1311 {
1312 	if (flag == 0) {
1313 		/* Get ctc table from current setup */
1314 		if (atomisp_css_get_ctc_table(asd, config))
1315 			return -EINVAL;
1316 	} else {
1317 		/* Set ctc table to isp parameters */
1318 		memcpy(&asd->params.css_param.ctc_table, config,
1319 		       sizeof(asd->params.css_param.ctc_table));
1320 		atomisp_css_set_ctc_table(asd, &asd->params.css_param.ctc_table);
1321 	}
1322 
1323 	return 0;
1324 }
1325 
1326 /*
1327  * Function to update gamma correction parameters
1328  */
atomisp_gamma_correction(struct atomisp_sub_device * asd,int flag,struct atomisp_gc_config * config)1329 int atomisp_gamma_correction(struct atomisp_sub_device *asd, int flag,
1330 			     struct atomisp_gc_config *config)
1331 {
1332 	if (flag == 0) {
1333 		/* Get gamma correction params from current setup */
1334 		if (atomisp_css_get_gc_config(asd, config))
1335 			return -EINVAL;
1336 	} else {
1337 		/* Set gamma correction params to isp parameters */
1338 		memcpy(&asd->params.css_param.gc_config, config,
1339 		       sizeof(asd->params.css_param.gc_config));
1340 		asd->params.config.gc_config = &asd->params.css_param.gc_config;
1341 		asd->params.css_update_params_needed = true;
1342 	}
1343 
1344 	return 0;
1345 }
1346 
1347 /*
1348  * Function to update narrow gamma flag
1349  */
atomisp_formats(struct atomisp_sub_device * asd,int flag,struct atomisp_formats_config * config)1350 int atomisp_formats(struct atomisp_sub_device *asd, int flag,
1351 		    struct atomisp_formats_config *config)
1352 {
1353 	if (flag == 0) {
1354 		/* Get narrow gamma flag from current setup */
1355 		if (atomisp_css_get_formats_config(asd, config))
1356 			return -EINVAL;
1357 	} else {
1358 		/* Set narrow gamma flag to isp parameters */
1359 		memcpy(&asd->params.css_param.formats_config, config,
1360 		       sizeof(asd->params.css_param.formats_config));
1361 		asd->params.config.formats_config = &asd->params.css_param.formats_config;
1362 	}
1363 
1364 	return 0;
1365 }
1366 
atomisp_free_internal_buffers(struct atomisp_sub_device * asd)1367 void atomisp_free_internal_buffers(struct atomisp_sub_device *asd)
1368 {
1369 	atomisp_free_css_parameters(&asd->params.css_param);
1370 }
1371 
atomisp_update_grid_info(struct atomisp_sub_device * asd,enum ia_css_pipe_id pipe_id)1372 static void atomisp_update_grid_info(struct atomisp_sub_device *asd,
1373 				     enum ia_css_pipe_id pipe_id)
1374 {
1375 	struct atomisp_device *isp = asd->isp;
1376 	int err;
1377 
1378 	if (atomisp_css_get_grid_info(asd, pipe_id))
1379 		return;
1380 
1381 	/* We must free all buffers because they no longer match
1382 	   the grid size. */
1383 	atomisp_css_free_stat_buffers(asd);
1384 
1385 	err = atomisp_alloc_css_stat_bufs(asd, ATOMISP_INPUT_STREAM_GENERAL);
1386 	if (err) {
1387 		dev_err(isp->dev, "stat_buf allocate error\n");
1388 		goto err;
1389 	}
1390 
1391 	if (atomisp_alloc_3a_output_buf(asd)) {
1392 		/* Failure for 3A buffers does not influence DIS buffers */
1393 		if (asd->params.s3a_output_bytes != 0) {
1394 			/* For SOC sensor happens s3a_output_bytes == 0,
1395 			 * using if condition to exclude false error log */
1396 			dev_err(isp->dev, "Failed to allocate memory for 3A statistics\n");
1397 		}
1398 		goto err;
1399 	}
1400 
1401 	if (atomisp_alloc_dis_coef_buf(asd)) {
1402 		dev_err(isp->dev,
1403 			"Failed to allocate memory for DIS statistics\n");
1404 		goto err;
1405 	}
1406 
1407 	if (atomisp_alloc_metadata_output_buf(asd)) {
1408 		dev_err(isp->dev, "Failed to allocate memory for metadata\n");
1409 		goto err;
1410 	}
1411 
1412 	return;
1413 
1414 err:
1415 	atomisp_css_free_stat_buffers(asd);
1416 	return;
1417 }
1418 
atomisp_curr_user_grid_info(struct atomisp_sub_device * asd,struct atomisp_grid_info * info)1419 static void atomisp_curr_user_grid_info(struct atomisp_sub_device *asd,
1420 					struct atomisp_grid_info *info)
1421 {
1422 	memcpy(info, &asd->params.curr_grid_info.s3a_grid,
1423 	       sizeof(struct ia_css_3a_grid_info));
1424 }
1425 
atomisp_compare_grid(struct atomisp_sub_device * asd,struct atomisp_grid_info * atomgrid)1426 int atomisp_compare_grid(struct atomisp_sub_device *asd,
1427 			 struct atomisp_grid_info *atomgrid)
1428 {
1429 	struct atomisp_grid_info tmp = {0};
1430 
1431 	atomisp_curr_user_grid_info(asd, &tmp);
1432 	return memcmp(atomgrid, &tmp, sizeof(tmp));
1433 }
1434 
1435 /*
1436  * Function to update Gdc table for gdc
1437  */
atomisp_gdc_cac_table(struct atomisp_sub_device * asd,int flag,struct atomisp_morph_table * config)1438 int atomisp_gdc_cac_table(struct atomisp_sub_device *asd, int flag,
1439 			  struct atomisp_morph_table *config)
1440 {
1441 	int ret;
1442 	int i;
1443 	struct atomisp_device *isp = asd->isp;
1444 
1445 	if (flag == 0) {
1446 		/* Get gdc table from current setup */
1447 		struct ia_css_morph_table tab = {0};
1448 
1449 		atomisp_css_get_morph_table(asd, &tab);
1450 
1451 		config->width = tab.width;
1452 		config->height = tab.height;
1453 
1454 		for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
1455 			ret = copy_to_user(config->coordinates_x[i],
1456 					   tab.coordinates_x[i], tab.height *
1457 					   tab.width * sizeof(*tab.coordinates_x[i]));
1458 			if (ret) {
1459 				dev_err(isp->dev,
1460 					"Failed to copy to User for x\n");
1461 				return -EFAULT;
1462 			}
1463 			ret = copy_to_user(config->coordinates_y[i],
1464 					   tab.coordinates_y[i], tab.height *
1465 					   tab.width * sizeof(*tab.coordinates_y[i]));
1466 			if (ret) {
1467 				dev_err(isp->dev,
1468 					"Failed to copy to User for y\n");
1469 				return -EFAULT;
1470 			}
1471 		}
1472 	} else {
1473 		struct ia_css_morph_table *tab =
1474 			    asd->params.css_param.morph_table;
1475 
1476 		/* free first if we have one */
1477 		if (tab) {
1478 			atomisp_css_morph_table_free(tab);
1479 			asd->params.css_param.morph_table = NULL;
1480 		}
1481 
1482 		/* allocate new one */
1483 		tab = atomisp_css_morph_table_allocate(config->width,
1484 						       config->height);
1485 
1486 		if (!tab) {
1487 			dev_err(isp->dev, "out of memory\n");
1488 			return -EINVAL;
1489 		}
1490 
1491 		for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
1492 			ret = copy_from_user(tab->coordinates_x[i],
1493 					     config->coordinates_x[i],
1494 					     config->height * config->width *
1495 					     sizeof(*config->coordinates_x[i]));
1496 			if (ret) {
1497 				dev_err(isp->dev,
1498 					"Failed to copy from User for x, ret %d\n",
1499 					ret);
1500 				atomisp_css_morph_table_free(tab);
1501 				return -EFAULT;
1502 			}
1503 			ret = copy_from_user(tab->coordinates_y[i],
1504 					     config->coordinates_y[i],
1505 					     config->height * config->width *
1506 					     sizeof(*config->coordinates_y[i]));
1507 			if (ret) {
1508 				dev_err(isp->dev,
1509 					"Failed to copy from User for y, ret is %d\n",
1510 					ret);
1511 				atomisp_css_morph_table_free(tab);
1512 				return -EFAULT;
1513 			}
1514 		}
1515 		asd->params.css_param.morph_table = tab;
1516 		if (asd->params.gdc_cac_en)
1517 			asd->params.config.morph_table = tab;
1518 	}
1519 
1520 	return 0;
1521 }
1522 
atomisp_macc_table(struct atomisp_sub_device * asd,int flag,struct atomisp_macc_config * config)1523 int atomisp_macc_table(struct atomisp_sub_device *asd, int flag,
1524 		       struct atomisp_macc_config *config)
1525 {
1526 	struct ia_css_macc_table *macc_table;
1527 
1528 	switch (config->color_effect) {
1529 	case V4L2_COLORFX_NONE:
1530 		macc_table = &asd->params.css_param.macc_table;
1531 		break;
1532 	case V4L2_COLORFX_SKY_BLUE:
1533 		macc_table = &blue_macc_table;
1534 		break;
1535 	case V4L2_COLORFX_GRASS_GREEN:
1536 		macc_table = &green_macc_table;
1537 		break;
1538 	case V4L2_COLORFX_SKIN_WHITEN_LOW:
1539 		macc_table = &skin_low_macc_table;
1540 		break;
1541 	case V4L2_COLORFX_SKIN_WHITEN:
1542 		macc_table = &skin_medium_macc_table;
1543 		break;
1544 	case V4L2_COLORFX_SKIN_WHITEN_HIGH:
1545 		macc_table = &skin_high_macc_table;
1546 		break;
1547 	default:
1548 		return -EINVAL;
1549 	}
1550 
1551 	if (flag == 0) {
1552 		/* Get macc table from current setup */
1553 		memcpy(&config->table, macc_table,
1554 		       sizeof(struct ia_css_macc_table));
1555 	} else {
1556 		memcpy(macc_table, &config->table,
1557 		       sizeof(struct ia_css_macc_table));
1558 		if (config->color_effect == asd->params.color_effect)
1559 			asd->params.config.macc_table = macc_table;
1560 	}
1561 
1562 	return 0;
1563 }
1564 
atomisp_set_dis_vector(struct atomisp_sub_device * asd,struct atomisp_dis_vector * vector)1565 int atomisp_set_dis_vector(struct atomisp_sub_device *asd,
1566 			   struct atomisp_dis_vector *vector)
1567 {
1568 	atomisp_css_video_set_dis_vector(asd, vector);
1569 
1570 	asd->params.dis_proj_data_valid = false;
1571 	asd->params.css_update_params_needed = true;
1572 	return 0;
1573 }
1574 
1575 /*
1576  * Function to set/get image stablization statistics
1577  */
atomisp_get_dis_stat(struct atomisp_sub_device * asd,struct atomisp_dis_statistics * stats)1578 int atomisp_get_dis_stat(struct atomisp_sub_device *asd,
1579 			 struct atomisp_dis_statistics *stats)
1580 {
1581 	return atomisp_css_get_dis_stat(asd, stats);
1582 }
1583 
1584 /*
1585  * Function  set camrea_prefiles.xml current sensor pixel array size
1586  */
atomisp_set_array_res(struct atomisp_sub_device * asd,struct atomisp_resolution * config)1587 int atomisp_set_array_res(struct atomisp_sub_device *asd,
1588 			  struct atomisp_resolution  *config)
1589 {
1590 	dev_dbg(asd->isp->dev, ">%s start\n", __func__);
1591 	if (!config) {
1592 		dev_err(asd->isp->dev, "Set sensor array size is not valid\n");
1593 		return -EINVAL;
1594 	}
1595 
1596 	asd->sensor_array_res.width = config->width;
1597 	asd->sensor_array_res.height = config->height;
1598 	return 0;
1599 }
1600 
1601 /*
1602  * Function to get DVS2 BQ resolution settings
1603  */
atomisp_get_dvs2_bq_resolutions(struct atomisp_sub_device * asd,struct atomisp_dvs2_bq_resolutions * bq_res)1604 int atomisp_get_dvs2_bq_resolutions(struct atomisp_sub_device *asd,
1605 				    struct atomisp_dvs2_bq_resolutions *bq_res)
1606 {
1607 	struct ia_css_pipe_config *pipe_cfg = NULL;
1608 
1609 	struct ia_css_stream *stream =
1610 		    asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
1611 	if (!stream) {
1612 		dev_warn(asd->isp->dev, "stream is not created");
1613 		return -EAGAIN;
1614 	}
1615 
1616 	pipe_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
1617 		   .pipe_configs[IA_CSS_PIPE_ID_VIDEO];
1618 
1619 	if (!bq_res)
1620 		return -EINVAL;
1621 
1622 	/* the GDC output resolution */
1623 	bq_res->output_bq.width_bq = pipe_cfg->output_info[0].res.width / 2;
1624 	bq_res->output_bq.height_bq = pipe_cfg->output_info[0].res.height / 2;
1625 
1626 	bq_res->envelope_bq.width_bq = 0;
1627 	bq_res->envelope_bq.height_bq = 0;
1628 	/* the GDC input resolution */
1629 	bq_res->source_bq.width_bq = bq_res->output_bq.width_bq +
1630 				     pipe_cfg->dvs_envelope.width / 2;
1631 	bq_res->source_bq.height_bq = bq_res->output_bq.height_bq +
1632 				      pipe_cfg->dvs_envelope.height / 2;
1633 	/*
1634 	 * Bad pixels caused by spatial filter processing
1635 	 * ISP filter resolution should be given by CSS/FW, but for now
1636 	 * there is not such API to query, and it is fixed value, so
1637 	 * hardcoded here.
1638 	 */
1639 	bq_res->ispfilter_bq.width_bq = 12 / 2;
1640 	bq_res->ispfilter_bq.height_bq = 12 / 2;
1641 	/* spatial filter shift, always 4 pixels */
1642 	bq_res->gdc_shift_bq.width_bq = 4 / 2;
1643 	bq_res->gdc_shift_bq.height_bq = 4 / 2;
1644 
1645 	if (asd->params.video_dis_en) {
1646 		bq_res->envelope_bq.width_bq = pipe_cfg->dvs_envelope.width / 2 -
1647 					       bq_res->ispfilter_bq.width_bq;
1648 		bq_res->envelope_bq.height_bq = pipe_cfg->dvs_envelope.height / 2 -
1649 						bq_res->ispfilter_bq.height_bq;
1650 	}
1651 
1652 	dev_dbg(asd->isp->dev,
1653 		"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",
1654 		bq_res->source_bq.width_bq, bq_res->source_bq.height_bq,
1655 		bq_res->ispfilter_bq.width_bq, bq_res->ispfilter_bq.height_bq,
1656 		bq_res->gdc_shift_bq.width_bq, bq_res->gdc_shift_bq.height_bq,
1657 		bq_res->envelope_bq.width_bq, bq_res->envelope_bq.height_bq,
1658 		bq_res->output_bq.width_bq, bq_res->output_bq.height_bq);
1659 
1660 	return 0;
1661 }
1662 
atomisp_set_dis_coefs(struct atomisp_sub_device * asd,struct atomisp_dis_coefficients * coefs)1663 int atomisp_set_dis_coefs(struct atomisp_sub_device *asd,
1664 			  struct atomisp_dis_coefficients *coefs)
1665 {
1666 	return atomisp_css_set_dis_coefs(asd, coefs);
1667 }
1668 
1669 /*
1670  * Function to set/get 3A stat from isp
1671  */
atomisp_3a_stat(struct atomisp_sub_device * asd,int flag,struct atomisp_3a_statistics * config)1672 int atomisp_3a_stat(struct atomisp_sub_device *asd, int flag,
1673 		    struct atomisp_3a_statistics *config)
1674 {
1675 	struct atomisp_device *isp = asd->isp;
1676 	struct atomisp_s3a_buf *s3a_buf;
1677 	unsigned long ret;
1678 
1679 	if (flag != 0)
1680 		return -EINVAL;
1681 
1682 	/* sanity check to avoid writing into unallocated memory. */
1683 	if (asd->params.s3a_output_bytes == 0)
1684 		return -EINVAL;
1685 
1686 	if (atomisp_compare_grid(asd, &config->grid_info) != 0) {
1687 		/* If the grid info in the argument differs from the current
1688 		   grid info, we tell the caller to reset the grid size and
1689 		   try again. */
1690 		return -EAGAIN;
1691 	}
1692 
1693 	if (list_empty(&asd->s3a_stats_ready)) {
1694 		dev_err(isp->dev, "3a statistics is not valid.\n");
1695 		return -EAGAIN;
1696 	}
1697 
1698 	s3a_buf = list_entry(asd->s3a_stats_ready.next,
1699 			     struct atomisp_s3a_buf, list);
1700 	if (s3a_buf->s3a_map)
1701 		ia_css_translate_3a_statistics(
1702 		    asd->params.s3a_user_stat, s3a_buf->s3a_map);
1703 	else
1704 		ia_css_get_3a_statistics(asd->params.s3a_user_stat,
1705 					 s3a_buf->s3a_data);
1706 
1707 	config->exp_id = s3a_buf->s3a_data->exp_id;
1708 	config->isp_config_id = s3a_buf->s3a_data->isp_config_id;
1709 
1710 	ret = copy_to_user(config->data, asd->params.s3a_user_stat->data,
1711 			   asd->params.s3a_output_bytes);
1712 	if (ret) {
1713 		dev_err(isp->dev, "copy to user failed: copied %lu bytes\n",
1714 			ret);
1715 		return -EFAULT;
1716 	}
1717 
1718 	/* Move to free buffer list */
1719 	list_del_init(&s3a_buf->list);
1720 	list_add_tail(&s3a_buf->list, &asd->s3a_stats);
1721 	dev_dbg(isp->dev, "%s: finish getting exp_id %d 3a stat, isp_config_id %d\n",
1722 		__func__,
1723 		config->exp_id, config->isp_config_id);
1724 	return 0;
1725 }
1726 
1727 /*
1728  * Function to calculate real zoom region for every pipe
1729  */
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)1730 int atomisp_calculate_real_zoom_region(struct atomisp_sub_device *asd,
1731 				       struct ia_css_dz_config   *dz_config,
1732 				       enum ia_css_pipe_id css_pipe_id)
1733 
1734 {
1735 	struct atomisp_stream_env *stream_env =
1736 		    &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL];
1737 	struct atomisp_resolution  eff_res, out_res;
1738 	int w_offset, h_offset;
1739 
1740 	memset(&eff_res, 0, sizeof(eff_res));
1741 	memset(&out_res, 0, sizeof(out_res));
1742 
1743 	if (dz_config->dx || dz_config->dy)
1744 		return 0;
1745 
1746 	if (css_pipe_id != IA_CSS_PIPE_ID_PREVIEW
1747 	    && css_pipe_id != IA_CSS_PIPE_ID_CAPTURE) {
1748 		dev_err(asd->isp->dev, "%s the set pipe no support crop region"
1749 			, __func__);
1750 		return -EINVAL;
1751 	}
1752 
1753 	eff_res.width =
1754 	    stream_env->stream_config.input_config.effective_res.width;
1755 	eff_res.height =
1756 	    stream_env->stream_config.input_config.effective_res.height;
1757 	if (eff_res.width == 0 || eff_res.height == 0) {
1758 		dev_err(asd->isp->dev, "%s err effective resolution"
1759 			, __func__);
1760 		return -EINVAL;
1761 	}
1762 
1763 	if (dz_config->zoom_region.resolution.width
1764 	    == asd->sensor_array_res.width
1765 	    || dz_config->zoom_region.resolution.height
1766 	    == asd->sensor_array_res.height) {
1767 		/*no need crop region*/
1768 		dz_config->zoom_region.origin.x = 0;
1769 		dz_config->zoom_region.origin.y = 0;
1770 		dz_config->zoom_region.resolution.width = eff_res.width;
1771 		dz_config->zoom_region.resolution.height = eff_res.height;
1772 		return 0;
1773 	}
1774 
1775 	/* FIXME:
1776 	 * This is not the correct implementation with Google's definition, due
1777 	 * to firmware limitation.
1778 	 * map real crop region base on above calculating base max crop region.
1779 	 */
1780 
1781 	if (!IS_ISP2401) {
1782 		dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
1783 						  * eff_res.width
1784 						  / asd->sensor_array_res.width;
1785 		dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
1786 						  * eff_res.height
1787 						  / asd->sensor_array_res.height;
1788 		dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
1789 							  * eff_res.width
1790 							  / asd->sensor_array_res.width;
1791 		dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
1792 							  * eff_res.height
1793 							  / asd->sensor_array_res.height;
1794 		/*
1795 		 * Set same ratio of crop region resolution and current pipe output
1796 		 * resolution
1797 		 */
1798 		out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
1799 		out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
1800 		if (out_res.width == 0 || out_res.height == 0) {
1801 			dev_err(asd->isp->dev, "%s err current pipe output resolution"
1802 				, __func__);
1803 			return -EINVAL;
1804 		}
1805 	} else {
1806 		out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
1807 		out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
1808 		if (out_res.width == 0 || out_res.height == 0) {
1809 			dev_err(asd->isp->dev, "%s err current pipe output resolution"
1810 				, __func__);
1811 			return -EINVAL;
1812 		}
1813 
1814 		if (asd->sensor_array_res.width * out_res.height
1815 		    < out_res.width * asd->sensor_array_res.height) {
1816 			h_offset = asd->sensor_array_res.height
1817 				   - asd->sensor_array_res.width
1818 				   * out_res.height / out_res.width;
1819 			h_offset = h_offset / 2;
1820 			if (dz_config->zoom_region.origin.y < h_offset)
1821 				dz_config->zoom_region.origin.y = 0;
1822 			else
1823 				dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y - h_offset;
1824 			w_offset = 0;
1825 		} else {
1826 			w_offset = asd->sensor_array_res.width
1827 				   - asd->sensor_array_res.height
1828 				   * out_res.width / out_res.height;
1829 			w_offset = w_offset / 2;
1830 			if (dz_config->zoom_region.origin.x < w_offset)
1831 				dz_config->zoom_region.origin.x = 0;
1832 			else
1833 				dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x - w_offset;
1834 			h_offset = 0;
1835 		}
1836 		dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
1837 						  * eff_res.width
1838 						  / (asd->sensor_array_res.width - 2 * w_offset);
1839 		dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
1840 						  * eff_res.height
1841 						  / (asd->sensor_array_res.height - 2 * h_offset);
1842 		dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
1843 						  * eff_res.width
1844 						  / (asd->sensor_array_res.width - 2 * w_offset);
1845 		dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
1846 						  * eff_res.height
1847 						  / (asd->sensor_array_res.height - 2 * h_offset);
1848 	}
1849 
1850 	if (out_res.width * dz_config->zoom_region.resolution.height
1851 	    > dz_config->zoom_region.resolution.width * out_res.height) {
1852 		dz_config->zoom_region.resolution.height =
1853 		    dz_config->zoom_region.resolution.width
1854 		    * out_res.height / out_res.width;
1855 	} else {
1856 		dz_config->zoom_region.resolution.width =
1857 		    dz_config->zoom_region.resolution.height
1858 		    * out_res.width / out_res.height;
1859 	}
1860 	dev_dbg(asd->isp->dev,
1861 		"%s crop region:(%d,%d),(%d,%d) eff_res(%d, %d) array_size(%d,%d) out_res(%d, %d)\n",
1862 		__func__, dz_config->zoom_region.origin.x,
1863 		dz_config->zoom_region.origin.y,
1864 		dz_config->zoom_region.resolution.width,
1865 		dz_config->zoom_region.resolution.height,
1866 		eff_res.width, eff_res.height,
1867 		asd->sensor_array_res.width,
1868 		asd->sensor_array_res.height,
1869 		out_res.width, out_res.height);
1870 
1871 	if ((dz_config->zoom_region.origin.x +
1872 	     dz_config->zoom_region.resolution.width
1873 	     > eff_res.width) ||
1874 	    (dz_config->zoom_region.origin.y +
1875 	     dz_config->zoom_region.resolution.height
1876 	     > eff_res.height))
1877 		return -EINVAL;
1878 
1879 	return 0;
1880 }
1881 
1882 /*
1883  * Function to check the zoom region whether is effective
1884  */
atomisp_check_zoom_region(struct atomisp_sub_device * asd,struct ia_css_dz_config * dz_config)1885 static bool atomisp_check_zoom_region(
1886     struct atomisp_sub_device *asd,
1887     struct ia_css_dz_config *dz_config)
1888 {
1889 	struct atomisp_resolution  config;
1890 	bool flag = false;
1891 	unsigned int w, h;
1892 
1893 	memset(&config, 0, sizeof(struct atomisp_resolution));
1894 
1895 	if (dz_config->dx && dz_config->dy)
1896 		return true;
1897 
1898 	config.width = asd->sensor_array_res.width;
1899 	config.height = asd->sensor_array_res.height;
1900 	w = dz_config->zoom_region.origin.x +
1901 	    dz_config->zoom_region.resolution.width;
1902 	h = dz_config->zoom_region.origin.y +
1903 	    dz_config->zoom_region.resolution.height;
1904 
1905 	if ((w <= config.width) && (h <= config.height) && w > 0 && h > 0)
1906 		flag = true;
1907 	else
1908 		/* setting error zoom region */
1909 		dev_err(asd->isp->dev,
1910 			"%s zoom region ERROR:dz_config:(%d,%d),(%d,%d)array_res(%d, %d)\n",
1911 			__func__, dz_config->zoom_region.origin.x,
1912 			dz_config->zoom_region.origin.y,
1913 			dz_config->zoom_region.resolution.width,
1914 			dz_config->zoom_region.resolution.height,
1915 			config.width, config.height);
1916 
1917 	return flag;
1918 }
1919 
atomisp_apply_css_parameters(struct atomisp_sub_device * asd,struct atomisp_css_params * css_param)1920 void atomisp_apply_css_parameters(
1921     struct atomisp_sub_device *asd,
1922     struct atomisp_css_params *css_param)
1923 {
1924 	if (css_param->update_flag.wb_config)
1925 		asd->params.config.wb_config = &css_param->wb_config;
1926 
1927 	if (css_param->update_flag.ob_config)
1928 		asd->params.config.ob_config = &css_param->ob_config;
1929 
1930 	if (css_param->update_flag.dp_config)
1931 		asd->params.config.dp_config = &css_param->dp_config;
1932 
1933 	if (css_param->update_flag.nr_config)
1934 		asd->params.config.nr_config = &css_param->nr_config;
1935 
1936 	if (css_param->update_flag.ee_config)
1937 		asd->params.config.ee_config = &css_param->ee_config;
1938 
1939 	if (css_param->update_flag.tnr_config)
1940 		asd->params.config.tnr_config = &css_param->tnr_config;
1941 
1942 	if (css_param->update_flag.a3a_config)
1943 		asd->params.config.s3a_config = &css_param->s3a_config;
1944 
1945 	if (css_param->update_flag.ctc_config)
1946 		asd->params.config.ctc_config = &css_param->ctc_config;
1947 
1948 	if (css_param->update_flag.cnr_config)
1949 		asd->params.config.cnr_config = &css_param->cnr_config;
1950 
1951 	if (css_param->update_flag.ecd_config)
1952 		asd->params.config.ecd_config = &css_param->ecd_config;
1953 
1954 	if (css_param->update_flag.ynr_config)
1955 		asd->params.config.ynr_config = &css_param->ynr_config;
1956 
1957 	if (css_param->update_flag.fc_config)
1958 		asd->params.config.fc_config = &css_param->fc_config;
1959 
1960 	if (css_param->update_flag.macc_config)
1961 		asd->params.config.macc_config = &css_param->macc_config;
1962 
1963 	if (css_param->update_flag.aa_config)
1964 		asd->params.config.aa_config = &css_param->aa_config;
1965 
1966 	if (css_param->update_flag.anr_config)
1967 		asd->params.config.anr_config = &css_param->anr_config;
1968 
1969 	if (css_param->update_flag.xnr_config)
1970 		asd->params.config.xnr_config = &css_param->xnr_config;
1971 
1972 	if (css_param->update_flag.yuv2rgb_cc_config)
1973 		asd->params.config.yuv2rgb_cc_config = &css_param->yuv2rgb_cc_config;
1974 
1975 	if (css_param->update_flag.rgb2yuv_cc_config)
1976 		asd->params.config.rgb2yuv_cc_config = &css_param->rgb2yuv_cc_config;
1977 
1978 	if (css_param->update_flag.macc_table)
1979 		asd->params.config.macc_table = &css_param->macc_table;
1980 
1981 	if (css_param->update_flag.xnr_table)
1982 		asd->params.config.xnr_table = &css_param->xnr_table;
1983 
1984 	if (css_param->update_flag.r_gamma_table)
1985 		asd->params.config.r_gamma_table = &css_param->r_gamma_table;
1986 
1987 	if (css_param->update_flag.g_gamma_table)
1988 		asd->params.config.g_gamma_table = &css_param->g_gamma_table;
1989 
1990 	if (css_param->update_flag.b_gamma_table)
1991 		asd->params.config.b_gamma_table = &css_param->b_gamma_table;
1992 
1993 	if (css_param->update_flag.anr_thres)
1994 		atomisp_css_set_anr_thres(asd, &css_param->anr_thres);
1995 
1996 	if (css_param->update_flag.shading_table)
1997 		asd->params.config.shading_table = css_param->shading_table;
1998 
1999 	if (css_param->update_flag.morph_table && asd->params.gdc_cac_en)
2000 		asd->params.config.morph_table = css_param->morph_table;
2001 
2002 	if (css_param->update_flag.dvs2_coefs) {
2003 		struct ia_css_dvs_grid_info *dvs_grid_info =
2004 		    atomisp_css_get_dvs_grid_info(
2005 			&asd->params.curr_grid_info);
2006 
2007 		if (dvs_grid_info && dvs_grid_info->enable)
2008 			atomisp_css_set_dvs2_coefs(asd, css_param->dvs2_coeff);
2009 	}
2010 
2011 	if (css_param->update_flag.dvs_6axis_config)
2012 		atomisp_css_set_dvs_6axis(asd, css_param->dvs_6axis);
2013 
2014 	atomisp_css_set_isp_config_id(asd, css_param->isp_config_id);
2015 	/*
2016 	 * These configurations are on used by ISP1.x, not for ISP2.x,
2017 	 * so do not handle them. see comments of ia_css_isp_config.
2018 	 * 1 cc_config
2019 	 * 2 ce_config
2020 	 * 3 de_config
2021 	 * 4 gc_config
2022 	 * 5 gamma_table
2023 	 * 6 ctc_table
2024 	 * 7 dvs_coefs
2025 	 */
2026 }
2027 
copy_from_compatible(void * to,const void * from,unsigned long n,bool from_user)2028 static unsigned int long copy_from_compatible(void *to, const void *from,
2029 	unsigned long n, bool from_user)
2030 {
2031 	if (from_user)
2032 		return copy_from_user(to, (void __user *)from, n);
2033 	else
2034 		memcpy(to, from, n);
2035 	return 0;
2036 }
2037 
atomisp_cp_general_isp_parameters(struct atomisp_sub_device * asd,struct atomisp_parameters * arg,struct atomisp_css_params * css_param,bool from_user)2038 int atomisp_cp_general_isp_parameters(struct atomisp_sub_device *asd,
2039 				      struct atomisp_parameters *arg,
2040 				      struct atomisp_css_params *css_param,
2041 				      bool from_user)
2042 {
2043 	struct atomisp_parameters *cur_config = &css_param->update_flag;
2044 
2045 	if (!arg || !asd || !css_param)
2046 		return -EINVAL;
2047 
2048 	if (arg->wb_config && (from_user || !cur_config->wb_config)) {
2049 		if (copy_from_compatible(&css_param->wb_config, arg->wb_config,
2050 					 sizeof(struct ia_css_wb_config),
2051 					 from_user))
2052 			return -EFAULT;
2053 		css_param->update_flag.wb_config =
2054 		    (struct atomisp_wb_config *)&css_param->wb_config;
2055 	}
2056 
2057 	if (arg->ob_config && (from_user || !cur_config->ob_config)) {
2058 		if (copy_from_compatible(&css_param->ob_config, arg->ob_config,
2059 					 sizeof(struct ia_css_ob_config),
2060 					 from_user))
2061 			return -EFAULT;
2062 		css_param->update_flag.ob_config =
2063 		    (struct atomisp_ob_config *)&css_param->ob_config;
2064 	}
2065 
2066 	if (arg->dp_config && (from_user || !cur_config->dp_config)) {
2067 		if (copy_from_compatible(&css_param->dp_config, arg->dp_config,
2068 					 sizeof(struct ia_css_dp_config),
2069 					 from_user))
2070 			return -EFAULT;
2071 		css_param->update_flag.dp_config =
2072 		    (struct atomisp_dp_config *)&css_param->dp_config;
2073 	}
2074 
2075 	if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
2076 		if (arg->dz_config && (from_user || !cur_config->dz_config)) {
2077 			if (copy_from_compatible(&css_param->dz_config,
2078 						 arg->dz_config,
2079 						 sizeof(struct ia_css_dz_config),
2080 						 from_user))
2081 				return -EFAULT;
2082 			if (!atomisp_check_zoom_region(asd,
2083 						       &css_param->dz_config)) {
2084 				dev_err(asd->isp->dev, "crop region error!");
2085 				return -EINVAL;
2086 			}
2087 			css_param->update_flag.dz_config =
2088 			    (struct atomisp_dz_config *)
2089 			    &css_param->dz_config;
2090 		}
2091 	}
2092 
2093 	if (arg->nr_config && (from_user || !cur_config->nr_config)) {
2094 		if (copy_from_compatible(&css_param->nr_config, arg->nr_config,
2095 					 sizeof(struct ia_css_nr_config),
2096 					 from_user))
2097 			return -EFAULT;
2098 		css_param->update_flag.nr_config =
2099 		    (struct atomisp_nr_config *)&css_param->nr_config;
2100 	}
2101 
2102 	if (arg->ee_config && (from_user || !cur_config->ee_config)) {
2103 		if (copy_from_compatible(&css_param->ee_config, arg->ee_config,
2104 					 sizeof(struct ia_css_ee_config),
2105 					 from_user))
2106 			return -EFAULT;
2107 		css_param->update_flag.ee_config =
2108 		    (struct atomisp_ee_config *)&css_param->ee_config;
2109 	}
2110 
2111 	if (arg->tnr_config && (from_user || !cur_config->tnr_config)) {
2112 		if (copy_from_compatible(&css_param->tnr_config,
2113 					 arg->tnr_config,
2114 					 sizeof(struct ia_css_tnr_config),
2115 					 from_user))
2116 			return -EFAULT;
2117 		css_param->update_flag.tnr_config =
2118 		    (struct atomisp_tnr_config *)
2119 		    &css_param->tnr_config;
2120 	}
2121 
2122 	if (arg->a3a_config && (from_user || !cur_config->a3a_config)) {
2123 		if (copy_from_compatible(&css_param->s3a_config,
2124 					 arg->a3a_config,
2125 					 sizeof(struct ia_css_3a_config),
2126 					 from_user))
2127 			return -EFAULT;
2128 		css_param->update_flag.a3a_config =
2129 		    (struct atomisp_3a_config *)&css_param->s3a_config;
2130 	}
2131 
2132 	if (arg->ctc_config && (from_user || !cur_config->ctc_config)) {
2133 		if (copy_from_compatible(&css_param->ctc_config,
2134 					 arg->ctc_config,
2135 					 sizeof(struct ia_css_ctc_config),
2136 					 from_user))
2137 			return -EFAULT;
2138 		css_param->update_flag.ctc_config =
2139 		    (struct atomisp_ctc_config *)
2140 		    &css_param->ctc_config;
2141 	}
2142 
2143 	if (arg->cnr_config && (from_user || !cur_config->cnr_config)) {
2144 		if (copy_from_compatible(&css_param->cnr_config,
2145 					 arg->cnr_config,
2146 					 sizeof(struct ia_css_cnr_config),
2147 					 from_user))
2148 			return -EFAULT;
2149 		css_param->update_flag.cnr_config =
2150 		    (struct atomisp_cnr_config *)
2151 		    &css_param->cnr_config;
2152 	}
2153 
2154 	if (arg->ecd_config && (from_user || !cur_config->ecd_config)) {
2155 		if (copy_from_compatible(&css_param->ecd_config,
2156 					 arg->ecd_config,
2157 					 sizeof(struct ia_css_ecd_config),
2158 					 from_user))
2159 			return -EFAULT;
2160 		css_param->update_flag.ecd_config =
2161 		    (struct atomisp_ecd_config *)
2162 		    &css_param->ecd_config;
2163 	}
2164 
2165 	if (arg->ynr_config && (from_user || !cur_config->ynr_config)) {
2166 		if (copy_from_compatible(&css_param->ynr_config,
2167 					 arg->ynr_config,
2168 					 sizeof(struct ia_css_ynr_config),
2169 					 from_user))
2170 			return -EFAULT;
2171 		css_param->update_flag.ynr_config =
2172 		    (struct atomisp_ynr_config *)
2173 		    &css_param->ynr_config;
2174 	}
2175 
2176 	if (arg->fc_config && (from_user || !cur_config->fc_config)) {
2177 		if (copy_from_compatible(&css_param->fc_config,
2178 					 arg->fc_config,
2179 					 sizeof(struct ia_css_fc_config),
2180 					 from_user))
2181 			return -EFAULT;
2182 		css_param->update_flag.fc_config =
2183 		    (struct atomisp_fc_config *)&css_param->fc_config;
2184 	}
2185 
2186 	if (arg->macc_config && (from_user || !cur_config->macc_config)) {
2187 		if (copy_from_compatible(&css_param->macc_config,
2188 					 arg->macc_config,
2189 					 sizeof(struct ia_css_macc_config),
2190 					 from_user))
2191 			return -EFAULT;
2192 		css_param->update_flag.macc_config =
2193 		    (struct atomisp_macc_config *)
2194 		    &css_param->macc_config;
2195 	}
2196 
2197 	if (arg->aa_config && (from_user || !cur_config->aa_config)) {
2198 		if (copy_from_compatible(&css_param->aa_config, arg->aa_config,
2199 					 sizeof(struct ia_css_aa_config),
2200 					 from_user))
2201 			return -EFAULT;
2202 		css_param->update_flag.aa_config =
2203 		    (struct atomisp_aa_config *)&css_param->aa_config;
2204 	}
2205 
2206 	if (arg->anr_config && (from_user || !cur_config->anr_config)) {
2207 		if (copy_from_compatible(&css_param->anr_config,
2208 					 arg->anr_config,
2209 					 sizeof(struct ia_css_anr_config),
2210 					 from_user))
2211 			return -EFAULT;
2212 		css_param->update_flag.anr_config =
2213 		    (struct atomisp_anr_config *)
2214 		    &css_param->anr_config;
2215 	}
2216 
2217 	if (arg->xnr_config && (from_user || !cur_config->xnr_config)) {
2218 		if (copy_from_compatible(&css_param->xnr_config,
2219 					 arg->xnr_config,
2220 					 sizeof(struct ia_css_xnr_config),
2221 					 from_user))
2222 			return -EFAULT;
2223 		css_param->update_flag.xnr_config =
2224 		    (struct atomisp_xnr_config *)
2225 		    &css_param->xnr_config;
2226 	}
2227 
2228 	if (arg->yuv2rgb_cc_config &&
2229 	    (from_user || !cur_config->yuv2rgb_cc_config)) {
2230 		if (copy_from_compatible(&css_param->yuv2rgb_cc_config,
2231 					 arg->yuv2rgb_cc_config,
2232 					 sizeof(struct ia_css_cc_config),
2233 					 from_user))
2234 			return -EFAULT;
2235 		css_param->update_flag.yuv2rgb_cc_config =
2236 		    (struct atomisp_cc_config *)
2237 		    &css_param->yuv2rgb_cc_config;
2238 	}
2239 
2240 	if (arg->rgb2yuv_cc_config &&
2241 	    (from_user || !cur_config->rgb2yuv_cc_config)) {
2242 		if (copy_from_compatible(&css_param->rgb2yuv_cc_config,
2243 					 arg->rgb2yuv_cc_config,
2244 					 sizeof(struct ia_css_cc_config),
2245 					 from_user))
2246 			return -EFAULT;
2247 		css_param->update_flag.rgb2yuv_cc_config =
2248 		    (struct atomisp_cc_config *)
2249 		    &css_param->rgb2yuv_cc_config;
2250 	}
2251 
2252 	if (arg->macc_table && (from_user || !cur_config->macc_table)) {
2253 		if (copy_from_compatible(&css_param->macc_table,
2254 					 arg->macc_table,
2255 					 sizeof(struct ia_css_macc_table),
2256 					 from_user))
2257 			return -EFAULT;
2258 		css_param->update_flag.macc_table =
2259 		    (struct atomisp_macc_table *)
2260 		    &css_param->macc_table;
2261 	}
2262 
2263 	if (arg->xnr_table && (from_user || !cur_config->xnr_table)) {
2264 		if (copy_from_compatible(&css_param->xnr_table,
2265 					 arg->xnr_table,
2266 					 sizeof(struct ia_css_xnr_table),
2267 					 from_user))
2268 			return -EFAULT;
2269 		css_param->update_flag.xnr_table =
2270 		    (struct atomisp_xnr_table *)&css_param->xnr_table;
2271 	}
2272 
2273 	if (arg->r_gamma_table && (from_user || !cur_config->r_gamma_table)) {
2274 		if (copy_from_compatible(&css_param->r_gamma_table,
2275 					 arg->r_gamma_table,
2276 					 sizeof(struct ia_css_rgb_gamma_table),
2277 					 from_user))
2278 			return -EFAULT;
2279 		css_param->update_flag.r_gamma_table =
2280 		    (struct atomisp_rgb_gamma_table *)
2281 		    &css_param->r_gamma_table;
2282 	}
2283 
2284 	if (arg->g_gamma_table && (from_user || !cur_config->g_gamma_table)) {
2285 		if (copy_from_compatible(&css_param->g_gamma_table,
2286 					 arg->g_gamma_table,
2287 					 sizeof(struct ia_css_rgb_gamma_table),
2288 					 from_user))
2289 			return -EFAULT;
2290 		css_param->update_flag.g_gamma_table =
2291 		    (struct atomisp_rgb_gamma_table *)
2292 		    &css_param->g_gamma_table;
2293 	}
2294 
2295 	if (arg->b_gamma_table && (from_user || !cur_config->b_gamma_table)) {
2296 		if (copy_from_compatible(&css_param->b_gamma_table,
2297 					 arg->b_gamma_table,
2298 					 sizeof(struct ia_css_rgb_gamma_table),
2299 					 from_user))
2300 			return -EFAULT;
2301 		css_param->update_flag.b_gamma_table =
2302 		    (struct atomisp_rgb_gamma_table *)
2303 		    &css_param->b_gamma_table;
2304 	}
2305 
2306 	if (arg->anr_thres && (from_user || !cur_config->anr_thres)) {
2307 		if (copy_from_compatible(&css_param->anr_thres, arg->anr_thres,
2308 					 sizeof(struct ia_css_anr_thres),
2309 					 from_user))
2310 			return -EFAULT;
2311 		css_param->update_flag.anr_thres =
2312 		    (struct atomisp_anr_thres *)&css_param->anr_thres;
2313 	}
2314 
2315 	if (from_user)
2316 		css_param->isp_config_id = arg->isp_config_id;
2317 	/*
2318 	 * These configurations are on used by ISP1.x, not for ISP2.x,
2319 	 * so do not handle them. see comments of ia_css_isp_config.
2320 	 * 1 cc_config
2321 	 * 2 ce_config
2322 	 * 3 de_config
2323 	 * 4 gc_config
2324 	 * 5 gamma_table
2325 	 * 6 ctc_table
2326 	 * 7 dvs_coefs
2327 	 */
2328 	return 0;
2329 }
2330 
atomisp_cp_lsc_table(struct atomisp_sub_device * asd,struct atomisp_shading_table * source_st,struct atomisp_css_params * css_param,bool from_user)2331 int atomisp_cp_lsc_table(struct atomisp_sub_device *asd,
2332 			 struct atomisp_shading_table *source_st,
2333 			 struct atomisp_css_params *css_param,
2334 			 bool from_user)
2335 {
2336 	unsigned int i;
2337 	unsigned int len_table;
2338 	struct ia_css_shading_table *shading_table;
2339 	struct ia_css_shading_table *old_table;
2340 	struct atomisp_shading_table *st, dest_st;
2341 
2342 	if (!source_st)
2343 		return 0;
2344 
2345 	if (!css_param)
2346 		return -EINVAL;
2347 
2348 	if (!from_user && css_param->update_flag.shading_table)
2349 		return 0;
2350 
2351 	if (IS_ISP2401) {
2352 		if (copy_from_compatible(&dest_st, source_st,
2353 					sizeof(struct atomisp_shading_table),
2354 					from_user)) {
2355 			dev_err(asd->isp->dev, "copy shading table failed!");
2356 			return -EFAULT;
2357 		}
2358 		st = &dest_st;
2359 	} else {
2360 		st = source_st;
2361 	}
2362 
2363 	old_table = css_param->shading_table;
2364 
2365 	/* user config is to disable the shading table. */
2366 	if (!st->enable) {
2367 		/* Generate a minimum table with enable = 0. */
2368 		shading_table = atomisp_css_shading_table_alloc(1, 1);
2369 		if (!shading_table)
2370 			return -ENOMEM;
2371 		shading_table->enable = 0;
2372 		goto set_lsc;
2373 	}
2374 
2375 	/* Setting a new table. Validate first - all tables must be set */
2376 	for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
2377 		if (!st->data[i]) {
2378 			dev_err(asd->isp->dev, "shading table validate failed");
2379 			return -EINVAL;
2380 		}
2381 	}
2382 
2383 	/* Shading table size per color */
2384 	if (st->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
2385 	    st->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) {
2386 		dev_err(asd->isp->dev, "shading table w/h validate failed!");
2387 		return -EINVAL;
2388 	}
2389 
2390 	shading_table = atomisp_css_shading_table_alloc(st->width, st->height);
2391 	if (!shading_table)
2392 		return -ENOMEM;
2393 
2394 	len_table = st->width * st->height * ATOMISP_SC_TYPE_SIZE;
2395 	for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
2396 		if (copy_from_compatible(shading_table->data[i],
2397 					 st->data[i], len_table, from_user)) {
2398 			atomisp_css_shading_table_free(shading_table);
2399 			return -EFAULT;
2400 		}
2401 	}
2402 	shading_table->sensor_width = st->sensor_width;
2403 	shading_table->sensor_height = st->sensor_height;
2404 	shading_table->fraction_bits = st->fraction_bits;
2405 	shading_table->enable = st->enable;
2406 
2407 	/* No need to update shading table if it is the same */
2408 	if (old_table &&
2409 	    old_table->sensor_width == shading_table->sensor_width &&
2410 	    old_table->sensor_height == shading_table->sensor_height &&
2411 	    old_table->width == shading_table->width &&
2412 	    old_table->height == shading_table->height &&
2413 	    old_table->fraction_bits == shading_table->fraction_bits &&
2414 	    old_table->enable == shading_table->enable) {
2415 		bool data_is_same = true;
2416 
2417 		for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
2418 			if (memcmp(shading_table->data[i], old_table->data[i],
2419 				   len_table) != 0) {
2420 				data_is_same = false;
2421 				break;
2422 			}
2423 		}
2424 
2425 		if (data_is_same) {
2426 			atomisp_css_shading_table_free(shading_table);
2427 			return 0;
2428 		}
2429 	}
2430 
2431 set_lsc:
2432 	/* set LSC to CSS */
2433 	css_param->shading_table = shading_table;
2434 	css_param->update_flag.shading_table = (struct atomisp_shading_table *)shading_table;
2435 	asd->params.sc_en = shading_table;
2436 
2437 	if (old_table)
2438 		atomisp_css_shading_table_free(old_table);
2439 
2440 	return 0;
2441 }
2442 
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)2443 int atomisp_css_cp_dvs2_coefs(struct atomisp_sub_device *asd,
2444 			      struct ia_css_dvs2_coefficients *coefs,
2445 			      struct atomisp_css_params *css_param,
2446 			      bool from_user)
2447 {
2448 	struct ia_css_dvs_grid_info *cur =
2449 	    atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
2450 	int dvs_hor_coef_bytes, dvs_ver_coef_bytes;
2451 	struct ia_css_dvs2_coefficients dvs2_coefs;
2452 
2453 	if (!coefs || !cur)
2454 		return 0;
2455 
2456 	if (!from_user && css_param->update_flag.dvs2_coefs)
2457 		return 0;
2458 
2459 	if (!IS_ISP2401) {
2460 		if (sizeof(*cur) != sizeof(coefs->grid) ||
2461 		    memcmp(&coefs->grid, cur, sizeof(coefs->grid))) {
2462 			dev_err(asd->isp->dev, "dvs grid mismatch!\n");
2463 			/* If the grid info in the argument differs from the current
2464 			grid info, we tell the caller to reset the grid size and
2465 			try again. */
2466 			return -EAGAIN;
2467 		}
2468 
2469 		if (!coefs->hor_coefs.odd_real ||
2470 		    !coefs->hor_coefs.odd_imag ||
2471 		    !coefs->hor_coefs.even_real ||
2472 		    !coefs->hor_coefs.even_imag ||
2473 		    !coefs->ver_coefs.odd_real ||
2474 		    !coefs->ver_coefs.odd_imag ||
2475 		    !coefs->ver_coefs.even_real ||
2476 		    !coefs->ver_coefs.even_imag)
2477 			return -EINVAL;
2478 
2479 		if (!css_param->dvs2_coeff) {
2480 			/* DIS coefficients. */
2481 			css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
2482 			if (!css_param->dvs2_coeff)
2483 				return -ENOMEM;
2484 		}
2485 
2486 		dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
2487 		dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
2488 		if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
2489 					coefs->hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
2490 		    copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
2491 					coefs->hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
2492 		    copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
2493 					coefs->hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
2494 		    copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
2495 					coefs->hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
2496 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
2497 					coefs->ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
2498 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
2499 					coefs->ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
2500 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
2501 					coefs->ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
2502 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
2503 					coefs->ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
2504 			ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
2505 			css_param->dvs2_coeff = NULL;
2506 			return -EFAULT;
2507 		}
2508 	} else {
2509 		if (copy_from_compatible(&dvs2_coefs, coefs,
2510 					sizeof(struct ia_css_dvs2_coefficients),
2511 					from_user)) {
2512 			dev_err(asd->isp->dev, "copy dvs2 coef failed");
2513 			return -EFAULT;
2514 		}
2515 
2516 		if (sizeof(*cur) != sizeof(dvs2_coefs.grid) ||
2517 		    memcmp(&dvs2_coefs.grid, cur, sizeof(dvs2_coefs.grid))) {
2518 			dev_err(asd->isp->dev, "dvs grid mismatch!\n");
2519 			/* If the grid info in the argument differs from the current
2520 			grid info, we tell the caller to reset the grid size and
2521 			try again. */
2522 			return -EAGAIN;
2523 		}
2524 
2525 		if (!dvs2_coefs.hor_coefs.odd_real ||
2526 		    !dvs2_coefs.hor_coefs.odd_imag ||
2527 		    !dvs2_coefs.hor_coefs.even_real ||
2528 		    !dvs2_coefs.hor_coefs.even_imag ||
2529 		    !dvs2_coefs.ver_coefs.odd_real ||
2530 		    !dvs2_coefs.ver_coefs.odd_imag ||
2531 		    !dvs2_coefs.ver_coefs.even_real ||
2532 		    !dvs2_coefs.ver_coefs.even_imag)
2533 			return -EINVAL;
2534 
2535 		if (!css_param->dvs2_coeff) {
2536 			/* DIS coefficients. */
2537 			css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
2538 			if (!css_param->dvs2_coeff)
2539 				return -ENOMEM;
2540 		}
2541 
2542 		dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
2543 		dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
2544 		if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
2545 					dvs2_coefs.hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
2546 		    copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
2547 					dvs2_coefs.hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
2548 		    copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
2549 					dvs2_coefs.hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
2550 		    copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
2551 					dvs2_coefs.hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
2552 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
2553 					dvs2_coefs.ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
2554 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
2555 					dvs2_coefs.ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
2556 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
2557 					dvs2_coefs.ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
2558 		    copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
2559 					dvs2_coefs.ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
2560 			ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
2561 			css_param->dvs2_coeff = NULL;
2562 			return -EFAULT;
2563 		}
2564 	}
2565 
2566 	css_param->update_flag.dvs2_coefs =
2567 	    (struct atomisp_dis_coefficients *)css_param->dvs2_coeff;
2568 	return 0;
2569 }
2570 
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)2571 int atomisp_cp_dvs_6axis_config(struct atomisp_sub_device *asd,
2572 				struct atomisp_dvs_6axis_config *source_6axis_config,
2573 				struct atomisp_css_params *css_param,
2574 				bool from_user)
2575 {
2576 	struct ia_css_dvs_6axis_config *dvs_6axis_config;
2577 	struct ia_css_dvs_6axis_config *old_6axis_config;
2578 	struct ia_css_stream *stream =
2579 		    asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
2580 	struct ia_css_dvs_grid_info *dvs_grid_info =
2581 	    atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
2582 	int ret = -EFAULT;
2583 
2584 	if (!stream) {
2585 		dev_err(asd->isp->dev, "%s: internal error!", __func__);
2586 		return -EINVAL;
2587 	}
2588 
2589 	if (!source_6axis_config || !dvs_grid_info)
2590 		return 0;
2591 
2592 	if (!dvs_grid_info->enable)
2593 		return 0;
2594 
2595 	if (!from_user && css_param->update_flag.dvs_6axis_config)
2596 		return 0;
2597 
2598 	/* check whether need to reallocate for 6 axis config */
2599 	old_6axis_config = css_param->dvs_6axis;
2600 	dvs_6axis_config = old_6axis_config;
2601 
2602 	if (IS_ISP2401) {
2603 		struct ia_css_dvs_6axis_config t_6axis_config;
2604 
2605 		if (copy_from_compatible(&t_6axis_config, source_6axis_config,
2606 					sizeof(struct atomisp_dvs_6axis_config),
2607 					from_user)) {
2608 			dev_err(asd->isp->dev, "copy morph table failed!");
2609 			return -EFAULT;
2610 		}
2611 
2612 		if (old_6axis_config &&
2613 		    (old_6axis_config->width_y != t_6axis_config.width_y ||
2614 		    old_6axis_config->height_y != t_6axis_config.height_y ||
2615 		    old_6axis_config->width_uv != t_6axis_config.width_uv ||
2616 		    old_6axis_config->height_uv != t_6axis_config.height_uv)) {
2617 			ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
2618 			css_param->dvs_6axis = NULL;
2619 
2620 			dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
2621 			if (!dvs_6axis_config)
2622 				return -ENOMEM;
2623 		} else if (!dvs_6axis_config) {
2624 			dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
2625 			if (!dvs_6axis_config)
2626 				return -ENOMEM;
2627 		}
2628 
2629 		dvs_6axis_config->exp_id = t_6axis_config.exp_id;
2630 
2631 		if (copy_from_compatible(dvs_6axis_config->xcoords_y,
2632 					t_6axis_config.xcoords_y,
2633 					t_6axis_config.width_y *
2634 					t_6axis_config.height_y *
2635 					sizeof(*dvs_6axis_config->xcoords_y),
2636 					from_user))
2637 			goto error;
2638 		if (copy_from_compatible(dvs_6axis_config->ycoords_y,
2639 					t_6axis_config.ycoords_y,
2640 					t_6axis_config.width_y *
2641 					t_6axis_config.height_y *
2642 					sizeof(*dvs_6axis_config->ycoords_y),
2643 					from_user))
2644 			goto error;
2645 		if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
2646 					t_6axis_config.xcoords_uv,
2647 					t_6axis_config.width_uv *
2648 					t_6axis_config.height_uv *
2649 					sizeof(*dvs_6axis_config->xcoords_uv),
2650 					from_user))
2651 			goto error;
2652 		if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
2653 					t_6axis_config.ycoords_uv,
2654 					t_6axis_config.width_uv *
2655 					t_6axis_config.height_uv *
2656 					sizeof(*dvs_6axis_config->ycoords_uv),
2657 					from_user))
2658 			goto error;
2659 	} else {
2660 		if (old_6axis_config &&
2661 		    (old_6axis_config->width_y != source_6axis_config->width_y ||
2662 		    old_6axis_config->height_y != source_6axis_config->height_y ||
2663 		    old_6axis_config->width_uv != source_6axis_config->width_uv ||
2664 		    old_6axis_config->height_uv != source_6axis_config->height_uv)) {
2665 			ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
2666 			css_param->dvs_6axis = NULL;
2667 
2668 			dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
2669 			if (!dvs_6axis_config) {
2670 				ret = -ENOMEM;
2671 				goto error;
2672 			}
2673 		} else if (!dvs_6axis_config) {
2674 			dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
2675 			if (!dvs_6axis_config) {
2676 				ret = -ENOMEM;
2677 				goto error;
2678 			}
2679 		}
2680 
2681 		dvs_6axis_config->exp_id = source_6axis_config->exp_id;
2682 
2683 		if (copy_from_compatible(dvs_6axis_config->xcoords_y,
2684 					source_6axis_config->xcoords_y,
2685 					source_6axis_config->width_y *
2686 					source_6axis_config->height_y *
2687 					sizeof(*source_6axis_config->xcoords_y),
2688 					from_user))
2689 			goto error;
2690 		if (copy_from_compatible(dvs_6axis_config->ycoords_y,
2691 					source_6axis_config->ycoords_y,
2692 					source_6axis_config->width_y *
2693 					source_6axis_config->height_y *
2694 					sizeof(*source_6axis_config->ycoords_y),
2695 					from_user))
2696 			goto error;
2697 		if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
2698 					source_6axis_config->xcoords_uv,
2699 					source_6axis_config->width_uv *
2700 					source_6axis_config->height_uv *
2701 					sizeof(*source_6axis_config->xcoords_uv),
2702 					from_user))
2703 			goto error;
2704 		if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
2705 					source_6axis_config->ycoords_uv,
2706 					source_6axis_config->width_uv *
2707 					source_6axis_config->height_uv *
2708 					sizeof(*source_6axis_config->ycoords_uv),
2709 					from_user))
2710 			goto error;
2711 	}
2712 	css_param->dvs_6axis = dvs_6axis_config;
2713 	css_param->update_flag.dvs_6axis_config =
2714 	    (struct atomisp_dvs_6axis_config *)dvs_6axis_config;
2715 	return 0;
2716 
2717 error:
2718 	if (dvs_6axis_config)
2719 		ia_css_dvs2_6axis_config_free(dvs_6axis_config);
2720 	return ret;
2721 }
2722 
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)2723 int atomisp_cp_morph_table(struct atomisp_sub_device *asd,
2724 			   struct atomisp_morph_table *source_morph_table,
2725 			   struct atomisp_css_params *css_param,
2726 			   bool from_user)
2727 {
2728 	int ret = -EFAULT;
2729 	unsigned int i;
2730 	struct ia_css_morph_table *morph_table;
2731 	struct ia_css_morph_table *old_morph_table;
2732 
2733 	if (!source_morph_table)
2734 		return 0;
2735 
2736 	if (!from_user && css_param->update_flag.morph_table)
2737 		return 0;
2738 
2739 	old_morph_table = css_param->morph_table;
2740 
2741 	if (IS_ISP2401) {
2742 		struct ia_css_morph_table mtbl;
2743 
2744 		if (copy_from_compatible(&mtbl, source_morph_table,
2745 				sizeof(struct atomisp_morph_table),
2746 				from_user)) {
2747 			dev_err(asd->isp->dev, "copy morph table failed!");
2748 			return -EFAULT;
2749 		}
2750 
2751 		morph_table = atomisp_css_morph_table_allocate(
2752 				mtbl.width,
2753 				mtbl.height);
2754 		if (!morph_table)
2755 			return -ENOMEM;
2756 
2757 		for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
2758 			if (copy_from_compatible(morph_table->coordinates_x[i],
2759 						(__force void *)source_morph_table->coordinates_x[i],
2760 						mtbl.height * mtbl.width *
2761 						sizeof(*morph_table->coordinates_x[i]),
2762 						from_user))
2763 				goto error;
2764 
2765 			if (copy_from_compatible(morph_table->coordinates_y[i],
2766 						(__force void *)source_morph_table->coordinates_y[i],
2767 						mtbl.height * mtbl.width *
2768 						sizeof(*morph_table->coordinates_y[i]),
2769 						from_user))
2770 				goto error;
2771 		}
2772 	} else {
2773 		morph_table = atomisp_css_morph_table_allocate(
2774 				source_morph_table->width,
2775 				source_morph_table->height);
2776 		if (!morph_table) {
2777 			ret = -ENOMEM;
2778 			goto error;
2779 		}
2780 
2781 		for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
2782 			if (copy_from_compatible(morph_table->coordinates_x[i],
2783 						(__force void *)source_morph_table->coordinates_x[i],
2784 						source_morph_table->height * source_morph_table->width *
2785 						sizeof(*source_morph_table->coordinates_x[i]),
2786 						from_user))
2787 				goto error;
2788 
2789 			if (copy_from_compatible(morph_table->coordinates_y[i],
2790 						(__force void *)source_morph_table->coordinates_y[i],
2791 						source_morph_table->height * source_morph_table->width *
2792 						sizeof(*source_morph_table->coordinates_y[i]),
2793 						from_user))
2794 				goto error;
2795 		}
2796 	}
2797 
2798 	css_param->morph_table = morph_table;
2799 	if (old_morph_table)
2800 		atomisp_css_morph_table_free(old_morph_table);
2801 	css_param->update_flag.morph_table =
2802 	    (struct atomisp_morph_table *)morph_table;
2803 	return 0;
2804 
2805 error:
2806 	if (morph_table)
2807 		atomisp_css_morph_table_free(morph_table);
2808 	return ret;
2809 }
2810 
atomisp_makeup_css_parameters(struct atomisp_sub_device * asd,struct atomisp_parameters * arg,struct atomisp_css_params * css_param)2811 int atomisp_makeup_css_parameters(struct atomisp_sub_device *asd,
2812 				  struct atomisp_parameters *arg,
2813 				  struct atomisp_css_params *css_param)
2814 {
2815 	int ret;
2816 
2817 	ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, false);
2818 	if (ret)
2819 		return ret;
2820 	ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, false);
2821 	if (ret)
2822 		return ret;
2823 	ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, false);
2824 	if (ret)
2825 		return ret;
2826 	ret = atomisp_css_cp_dvs2_coefs(asd,
2827 					(struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
2828 					css_param, false);
2829 	if (ret)
2830 		return ret;
2831 	ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
2832 					  css_param, false);
2833 	return ret;
2834 }
2835 
atomisp_free_css_parameters(struct atomisp_css_params * css_param)2836 void atomisp_free_css_parameters(struct atomisp_css_params *css_param)
2837 {
2838 	if (css_param->dvs_6axis) {
2839 		ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
2840 		css_param->dvs_6axis = NULL;
2841 	}
2842 	if (css_param->dvs2_coeff) {
2843 		ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
2844 		css_param->dvs2_coeff = NULL;
2845 	}
2846 	if (css_param->shading_table) {
2847 		ia_css_shading_table_free(css_param->shading_table);
2848 		css_param->shading_table = NULL;
2849 	}
2850 	if (css_param->morph_table) {
2851 		ia_css_morph_table_free(css_param->morph_table);
2852 		css_param->morph_table = NULL;
2853 	}
2854 }
2855 
atomisp_move_frame_to_activeq(struct ia_css_frame * frame,struct atomisp_css_params_with_list * param)2856 static void atomisp_move_frame_to_activeq(struct ia_css_frame *frame,
2857 					  struct atomisp_css_params_with_list *param)
2858 {
2859 	struct atomisp_video_pipe *pipe = vb_to_pipe(&frame->vb.vb2_buf);
2860 	unsigned long irqflags;
2861 
2862 	pipe->frame_params[frame->vb.vb2_buf.index] = param;
2863 	spin_lock_irqsave(&pipe->irq_lock, irqflags);
2864 	list_move_tail(&frame->queue, &pipe->activeq);
2865 	spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
2866 }
2867 
2868 /*
2869  * Check parameter queue list and buffer queue list to find out if matched items
2870  * and then set parameter to CSS and enqueue buffer to CSS.
2871  * Of course, if the buffer in buffer waiting list is not bound to a per-frame
2872  * parameter, it will be enqueued into CSS as long as the per-frame setting
2873  * buffers before it get enqueued.
2874  */
atomisp_handle_parameter_and_buffer(struct atomisp_video_pipe * pipe)2875 void atomisp_handle_parameter_and_buffer(struct atomisp_video_pipe *pipe)
2876 {
2877 	struct atomisp_sub_device *asd = pipe->asd;
2878 	struct ia_css_frame *frame = NULL, *frame_tmp;
2879 	struct atomisp_css_params_with_list *param = NULL, *param_tmp;
2880 	bool need_to_enqueue_buffer = false;
2881 	int i;
2882 
2883 	lockdep_assert_held(&asd->isp->mutex);
2884 
2885 	/*
2886 	 * CSS/FW requires set parameter and enqueue buffer happen after ISP
2887 	 * is streamon.
2888 	 */
2889 	if (!asd->streaming)
2890 		return;
2891 
2892 	if (list_empty(&pipe->per_frame_params) ||
2893 	    list_empty(&pipe->buffers_waiting_for_param))
2894 		return;
2895 
2896 	list_for_each_entry_safe(frame, frame_tmp,
2897 				 &pipe->buffers_waiting_for_param, queue) {
2898 		i = frame->vb.vb2_buf.index;
2899 		if (pipe->frame_request_config_id[i]) {
2900 			list_for_each_entry_safe(param, param_tmp,
2901 						 &pipe->per_frame_params, list) {
2902 				if (pipe->frame_request_config_id[i] != param->params.isp_config_id)
2903 					continue;
2904 
2905 				list_del(&param->list);
2906 
2907 				/*
2908 				 * clear the request config id as the buffer
2909 				 * will be handled and enqueued into CSS soon
2910 				 */
2911 				pipe->frame_request_config_id[i] = 0;
2912 				atomisp_move_frame_to_activeq(frame, param);
2913 				need_to_enqueue_buffer = true;
2914 				break;
2915 			}
2916 
2917 			/* If this is the end, stop further loop */
2918 			if (list_entry_is_head(param, &pipe->per_frame_params, list))
2919 				break;
2920 		} else {
2921 			atomisp_move_frame_to_activeq(frame, NULL);
2922 			need_to_enqueue_buffer = true;
2923 		}
2924 	}
2925 
2926 	if (!need_to_enqueue_buffer)
2927 		return;
2928 
2929 	atomisp_qbuffers_to_css(asd);
2930 }
2931 
2932 /*
2933  * Function to configure ISP parameters
2934  */
atomisp_set_parameters(struct video_device * vdev,struct atomisp_parameters * arg)2935 int atomisp_set_parameters(struct video_device *vdev,
2936 			   struct atomisp_parameters *arg)
2937 {
2938 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
2939 	struct atomisp_sub_device *asd = pipe->asd;
2940 	struct atomisp_css_params_with_list *param = NULL;
2941 	struct atomisp_css_params *css_param = &asd->params.css_param;
2942 	int ret;
2943 
2944 	lockdep_assert_held(&asd->isp->mutex);
2945 
2946 	if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream) {
2947 		dev_err(asd->isp->dev, "%s: internal error!\n", __func__);
2948 		return -EINVAL;
2949 	}
2950 
2951 	dev_dbg(asd->isp->dev, "set parameter(per_frame_setting %d) isp_config_id %d of %s\n",
2952 		arg->per_frame_setting, arg->isp_config_id, vdev->name);
2953 
2954 	if (arg->per_frame_setting) {
2955 		/*
2956 		 * Per-frame setting enabled, we allocate a new parameter
2957 		 * buffer to cache the parameters and only when frame buffers
2958 		 * are ready, the parameters will be set to CSS.
2959 		 * per-frame setting only works for the main output frame.
2960 		 */
2961 		param = kvzalloc(sizeof(*param), GFP_KERNEL);
2962 		if (!param) {
2963 			dev_err(asd->isp->dev, "%s: failed to alloc params buffer\n",
2964 				__func__);
2965 			return -ENOMEM;
2966 		}
2967 		css_param = &param->params;
2968 	}
2969 
2970 	ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, true);
2971 	if (ret)
2972 		goto apply_parameter_failed;
2973 
2974 	ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, true);
2975 	if (ret)
2976 		goto apply_parameter_failed;
2977 
2978 	ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, true);
2979 	if (ret)
2980 		goto apply_parameter_failed;
2981 
2982 	ret = atomisp_css_cp_dvs2_coefs(asd,
2983 					(struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
2984 					css_param, true);
2985 	if (ret)
2986 		goto apply_parameter_failed;
2987 
2988 	ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
2989 					  css_param, true);
2990 	if (ret)
2991 		goto apply_parameter_failed;
2992 
2993 	if (!arg->per_frame_setting) {
2994 		/* indicate to CSS that we have parameters to be updated */
2995 		asd->params.css_update_params_needed = true;
2996 	} else {
2997 		list_add_tail(&param->list, &pipe->per_frame_params);
2998 		atomisp_handle_parameter_and_buffer(pipe);
2999 	}
3000 
3001 	return 0;
3002 
3003 apply_parameter_failed:
3004 	if (css_param)
3005 		atomisp_free_css_parameters(css_param);
3006 	kvfree(param);
3007 
3008 	return ret;
3009 }
3010 
3011 /*
3012  * Function to set/get isp parameters to isp
3013  */
atomisp_param(struct atomisp_sub_device * asd,int flag,struct atomisp_parm * config)3014 int atomisp_param(struct atomisp_sub_device *asd, int flag,
3015 		  struct atomisp_parm *config)
3016 {
3017 	struct ia_css_pipe_config *vp_cfg =
3018 		    &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
3019 		    pipe_configs[IA_CSS_PIPE_ID_VIDEO];
3020 
3021 	/* Read parameter for 3A binary info */
3022 	if (flag == 0) {
3023 		struct ia_css_dvs_grid_info *dvs_grid_info =
3024 		    atomisp_css_get_dvs_grid_info(
3025 			&asd->params.curr_grid_info);
3026 
3027 		atomisp_curr_user_grid_info(asd, &config->info);
3028 
3029 		/* We always return the resolution and stride even if there is
3030 		 * no valid metadata. This allows the caller to get the
3031 		 * information needed to allocate user-space buffers. */
3032 		config->metadata_config.metadata_height = asd->
3033 			stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
3034 			metadata_info.resolution.height;
3035 		config->metadata_config.metadata_stride = asd->
3036 			stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
3037 			metadata_info.stride;
3038 
3039 		/* update dvs grid info */
3040 		if (dvs_grid_info)
3041 			memcpy(&config->dvs_grid,
3042 			       dvs_grid_info,
3043 			       sizeof(struct ia_css_dvs_grid_info));
3044 
3045 		if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
3046 			config->dvs_envelop.width = 0;
3047 			config->dvs_envelop.height = 0;
3048 			return 0;
3049 		}
3050 
3051 		/* update dvs envelop info */
3052 		config->dvs_envelop.width = vp_cfg->dvs_envelope.width;
3053 		config->dvs_envelop.height = vp_cfg->dvs_envelope.height;
3054 		return 0;
3055 	}
3056 
3057 	memcpy(&asd->params.css_param.wb_config, &config->wb_config,
3058 	       sizeof(struct ia_css_wb_config));
3059 	memcpy(&asd->params.css_param.ob_config, &config->ob_config,
3060 	       sizeof(struct ia_css_ob_config));
3061 	memcpy(&asd->params.css_param.dp_config, &config->dp_config,
3062 	       sizeof(struct ia_css_dp_config));
3063 	memcpy(&asd->params.css_param.de_config, &config->de_config,
3064 	       sizeof(struct ia_css_de_config));
3065 	memcpy(&asd->params.css_param.dz_config, &config->dz_config,
3066 	       sizeof(struct ia_css_dz_config));
3067 	memcpy(&asd->params.css_param.ce_config, &config->ce_config,
3068 	       sizeof(struct ia_css_ce_config));
3069 	memcpy(&asd->params.css_param.nr_config, &config->nr_config,
3070 	       sizeof(struct ia_css_nr_config));
3071 	memcpy(&asd->params.css_param.ee_config, &config->ee_config,
3072 	       sizeof(struct ia_css_ee_config));
3073 	memcpy(&asd->params.css_param.tnr_config, &config->tnr_config,
3074 	       sizeof(struct ia_css_tnr_config));
3075 
3076 	if (asd->params.color_effect == V4L2_COLORFX_NEGATIVE) {
3077 		asd->params.css_param.cc_config.matrix[3] = -config->cc_config.matrix[3];
3078 		asd->params.css_param.cc_config.matrix[4] = -config->cc_config.matrix[4];
3079 		asd->params.css_param.cc_config.matrix[5] = -config->cc_config.matrix[5];
3080 		asd->params.css_param.cc_config.matrix[6] = -config->cc_config.matrix[6];
3081 		asd->params.css_param.cc_config.matrix[7] = -config->cc_config.matrix[7];
3082 		asd->params.css_param.cc_config.matrix[8] = -config->cc_config.matrix[8];
3083 	}
3084 
3085 	if (asd->params.color_effect != V4L2_COLORFX_SEPIA &&
3086 	    asd->params.color_effect != V4L2_COLORFX_BW) {
3087 		memcpy(&asd->params.css_param.cc_config, &config->cc_config,
3088 		       sizeof(struct ia_css_cc_config));
3089 		asd->params.config.cc_config = &asd->params.css_param.cc_config;
3090 	}
3091 
3092 	asd->params.config.wb_config = &asd->params.css_param.wb_config;
3093 	asd->params.config.ob_config = &asd->params.css_param.ob_config;
3094 	asd->params.config.de_config = &asd->params.css_param.de_config;
3095 	asd->params.config.dz_config = &asd->params.css_param.dz_config;
3096 	asd->params.config.ce_config = &asd->params.css_param.ce_config;
3097 	asd->params.config.dp_config = &asd->params.css_param.dp_config;
3098 	asd->params.config.nr_config = &asd->params.css_param.nr_config;
3099 	asd->params.config.ee_config = &asd->params.css_param.ee_config;
3100 	asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
3101 	asd->params.css_update_params_needed = true;
3102 
3103 	return 0;
3104 }
3105 
3106 /*
3107  * Function to configure color effect of the image
3108  */
atomisp_color_effect(struct atomisp_sub_device * asd,int flag,__s32 * effect)3109 int atomisp_color_effect(struct atomisp_sub_device *asd, int flag,
3110 			 __s32 *effect)
3111 {
3112 	struct ia_css_cc_config *cc_config = NULL;
3113 	struct ia_css_macc_table *macc_table = NULL;
3114 	struct ia_css_ctc_table *ctc_table = NULL;
3115 	int ret = 0;
3116 	struct v4l2_control control;
3117 	struct atomisp_device *isp = asd->isp;
3118 
3119 	if (flag == 0) {
3120 		*effect = asd->params.color_effect;
3121 		return 0;
3122 	}
3123 
3124 	control.id = V4L2_CID_COLORFX;
3125 	control.value = *effect;
3126 	ret =
3127 	    v4l2_s_ctrl(NULL, isp->inputs[asd->input_curr].camera->ctrl_handler,
3128 			&control);
3129 	/*
3130 	 * if set color effect to sensor successfully, return
3131 	 * 0 directly.
3132 	 */
3133 	if (!ret) {
3134 		asd->params.color_effect = (u32)*effect;
3135 		return 0;
3136 	}
3137 
3138 	if (*effect == asd->params.color_effect)
3139 		return 0;
3140 
3141 	/*
3142 	 * isp_subdev->params.macc_en should be set to false.
3143 	 */
3144 	asd->params.macc_en = false;
3145 
3146 	switch (*effect) {
3147 	case V4L2_COLORFX_NONE:
3148 		macc_table = &asd->params.css_param.macc_table;
3149 		asd->params.macc_en = true;
3150 		break;
3151 	case V4L2_COLORFX_SEPIA:
3152 		cc_config = &sepia_cc_config;
3153 		break;
3154 	case V4L2_COLORFX_NEGATIVE:
3155 		cc_config = &nega_cc_config;
3156 		break;
3157 	case V4L2_COLORFX_BW:
3158 		cc_config = &mono_cc_config;
3159 		break;
3160 	case V4L2_COLORFX_SKY_BLUE:
3161 		macc_table = &blue_macc_table;
3162 		asd->params.macc_en = true;
3163 		break;
3164 	case V4L2_COLORFX_GRASS_GREEN:
3165 		macc_table = &green_macc_table;
3166 		asd->params.macc_en = true;
3167 		break;
3168 	case V4L2_COLORFX_SKIN_WHITEN_LOW:
3169 		macc_table = &skin_low_macc_table;
3170 		asd->params.macc_en = true;
3171 		break;
3172 	case V4L2_COLORFX_SKIN_WHITEN:
3173 		macc_table = &skin_medium_macc_table;
3174 		asd->params.macc_en = true;
3175 		break;
3176 	case V4L2_COLORFX_SKIN_WHITEN_HIGH:
3177 		macc_table = &skin_high_macc_table;
3178 		asd->params.macc_en = true;
3179 		break;
3180 	case V4L2_COLORFX_VIVID:
3181 		ctc_table = &vivid_ctc_table;
3182 		break;
3183 	default:
3184 		return -EINVAL;
3185 	}
3186 	atomisp_update_capture_mode(asd);
3187 
3188 	if (cc_config)
3189 		asd->params.config.cc_config = cc_config;
3190 	if (macc_table)
3191 		asd->params.config.macc_table = macc_table;
3192 	if (ctc_table)
3193 		atomisp_css_set_ctc_table(asd, ctc_table);
3194 	asd->params.color_effect = (u32)*effect;
3195 	asd->params.css_update_params_needed = true;
3196 	return 0;
3197 }
3198 
3199 /*
3200  * Function to configure bad pixel correction
3201  */
atomisp_bad_pixel(struct atomisp_sub_device * asd,int flag,__s32 * value)3202 int atomisp_bad_pixel(struct atomisp_sub_device *asd, int flag,
3203 		      __s32 *value)
3204 {
3205 	if (flag == 0) {
3206 		*value = asd->params.bad_pixel_en;
3207 		return 0;
3208 	}
3209 	asd->params.bad_pixel_en = !!*value;
3210 
3211 	return 0;
3212 }
3213 
3214 /*
3215  * Function to configure bad pixel correction params
3216  */
atomisp_bad_pixel_param(struct atomisp_sub_device * asd,int flag,struct atomisp_dp_config * config)3217 int atomisp_bad_pixel_param(struct atomisp_sub_device *asd, int flag,
3218 			    struct atomisp_dp_config *config)
3219 {
3220 	if (flag == 0) {
3221 		/* Get bad pixel from current setup */
3222 		if (atomisp_css_get_dp_config(asd, config))
3223 			return -EINVAL;
3224 	} else {
3225 		/* Set bad pixel to isp parameters */
3226 		memcpy(&asd->params.css_param.dp_config, config,
3227 		       sizeof(asd->params.css_param.dp_config));
3228 		asd->params.config.dp_config = &asd->params.css_param.dp_config;
3229 		asd->params.css_update_params_needed = true;
3230 	}
3231 
3232 	return 0;
3233 }
3234 
3235 /*
3236  * Function to enable/disable video image stablization
3237  */
atomisp_video_stable(struct atomisp_sub_device * asd,int flag,__s32 * value)3238 int atomisp_video_stable(struct atomisp_sub_device *asd, int flag,
3239 			 __s32 *value)
3240 {
3241 	if (flag == 0)
3242 		*value = asd->params.video_dis_en;
3243 	else
3244 		asd->params.video_dis_en = !!*value;
3245 
3246 	return 0;
3247 }
3248 
3249 /*
3250  * Function to configure fixed pattern noise
3251  */
atomisp_fixed_pattern(struct atomisp_sub_device * asd,int flag,__s32 * value)3252 int atomisp_fixed_pattern(struct atomisp_sub_device *asd, int flag,
3253 			  __s32 *value)
3254 {
3255 	if (flag == 0) {
3256 		*value = asd->params.fpn_en;
3257 		return 0;
3258 	}
3259 
3260 	if (*value == 0) {
3261 		asd->params.fpn_en = false;
3262 		return 0;
3263 	}
3264 
3265 	/* Add function to get black from sensor with shutter off */
3266 	return 0;
3267 }
3268 
3269 static unsigned int
atomisp_bytesperline_to_padded_width(unsigned int bytesperline,enum ia_css_frame_format format)3270 atomisp_bytesperline_to_padded_width(unsigned int bytesperline,
3271 				     enum ia_css_frame_format format)
3272 {
3273 	switch (format) {
3274 	case IA_CSS_FRAME_FORMAT_UYVY:
3275 	case IA_CSS_FRAME_FORMAT_YUYV:
3276 	case IA_CSS_FRAME_FORMAT_RAW:
3277 	case IA_CSS_FRAME_FORMAT_RGB565:
3278 		return bytesperline / 2;
3279 	case IA_CSS_FRAME_FORMAT_RGBA888:
3280 		return bytesperline / 4;
3281 	/* The following cases could be removed, but we leave them
3282 	   in to document the formats that are included. */
3283 	case IA_CSS_FRAME_FORMAT_NV11:
3284 	case IA_CSS_FRAME_FORMAT_NV12:
3285 	case IA_CSS_FRAME_FORMAT_NV16:
3286 	case IA_CSS_FRAME_FORMAT_NV21:
3287 	case IA_CSS_FRAME_FORMAT_NV61:
3288 	case IA_CSS_FRAME_FORMAT_YV12:
3289 	case IA_CSS_FRAME_FORMAT_YV16:
3290 	case IA_CSS_FRAME_FORMAT_YUV420:
3291 	case IA_CSS_FRAME_FORMAT_YUV420_16:
3292 	case IA_CSS_FRAME_FORMAT_YUV422:
3293 	case IA_CSS_FRAME_FORMAT_YUV422_16:
3294 	case IA_CSS_FRAME_FORMAT_YUV444:
3295 	case IA_CSS_FRAME_FORMAT_YUV_LINE:
3296 	case IA_CSS_FRAME_FORMAT_PLANAR_RGB888:
3297 	case IA_CSS_FRAME_FORMAT_QPLANE6:
3298 	case IA_CSS_FRAME_FORMAT_BINARY_8:
3299 	default:
3300 		return bytesperline;
3301 	}
3302 }
3303 
3304 static int
atomisp_v4l2_framebuffer_to_css_frame(const struct v4l2_framebuffer * arg,struct ia_css_frame ** result)3305 atomisp_v4l2_framebuffer_to_css_frame(const struct v4l2_framebuffer *arg,
3306 				      struct ia_css_frame **result)
3307 {
3308 	struct ia_css_frame *res = NULL;
3309 	unsigned int padded_width;
3310 	enum ia_css_frame_format sh_format;
3311 	char *tmp_buf = NULL;
3312 	int ret = 0;
3313 
3314 	sh_format = v4l2_fmt_to_sh_fmt(arg->fmt.pixelformat);
3315 	padded_width = atomisp_bytesperline_to_padded_width(
3316 			   arg->fmt.bytesperline, sh_format);
3317 
3318 	/* Note: the padded width on an ia_css_frame is in elements, not in
3319 	   bytes. The RAW frame we use here should always be a 16bit RAW
3320 	   frame. This is why we bytesperline/2 is equal to the padded with */
3321 	if (ia_css_frame_allocate(&res, arg->fmt.width, arg->fmt.height,
3322 				       sh_format, padded_width, 0)) {
3323 		ret = -ENOMEM;
3324 		goto err;
3325 	}
3326 
3327 	tmp_buf = vmalloc(arg->fmt.sizeimage);
3328 	if (!tmp_buf) {
3329 		ret = -ENOMEM;
3330 		goto err;
3331 	}
3332 	if (copy_from_user(tmp_buf, (void __user __force *)arg->base,
3333 			   arg->fmt.sizeimage)) {
3334 		ret = -EFAULT;
3335 		goto err;
3336 	}
3337 
3338 	if (hmm_store(res->data, tmp_buf, arg->fmt.sizeimage)) {
3339 		ret = -EINVAL;
3340 		goto err;
3341 	}
3342 
3343 err:
3344 	if (ret && res)
3345 		ia_css_frame_free(res);
3346 	vfree(tmp_buf);
3347 	if (ret == 0)
3348 		*result = res;
3349 	return ret;
3350 }
3351 
3352 /*
3353  * Function to configure fixed pattern noise table
3354  */
atomisp_fixed_pattern_table(struct atomisp_sub_device * asd,struct v4l2_framebuffer * arg)3355 int atomisp_fixed_pattern_table(struct atomisp_sub_device *asd,
3356 				struct v4l2_framebuffer *arg)
3357 {
3358 	struct ia_css_frame *raw_black_frame = NULL;
3359 	int ret;
3360 
3361 	if (!arg)
3362 		return -EINVAL;
3363 
3364 	ret = atomisp_v4l2_framebuffer_to_css_frame(arg, &raw_black_frame);
3365 	if (ret)
3366 		return ret;
3367 
3368 	if (sh_css_set_black_frame(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream,
3369 				   raw_black_frame) != 0)
3370 		return -ENOMEM;
3371 
3372 	ia_css_frame_free(raw_black_frame);
3373 	return ret;
3374 }
3375 
3376 /*
3377  * Function to configure false color correction
3378  */
atomisp_false_color(struct atomisp_sub_device * asd,int flag,__s32 * value)3379 int atomisp_false_color(struct atomisp_sub_device *asd, int flag,
3380 			__s32 *value)
3381 {
3382 	/* Get nr config from current setup */
3383 	if (flag == 0) {
3384 		*value = asd->params.false_color;
3385 		return 0;
3386 	}
3387 
3388 	/* Set nr config to isp parameters */
3389 	if (*value) {
3390 		asd->params.config.de_config = NULL;
3391 	} else {
3392 		asd->params.css_param.de_config.pixelnoise = 0;
3393 		asd->params.config.de_config = &asd->params.css_param.de_config;
3394 	}
3395 	asd->params.css_update_params_needed = true;
3396 	asd->params.false_color = *value;
3397 	return 0;
3398 }
3399 
3400 /*
3401  * Function to configure bad pixel correction params
3402  */
atomisp_false_color_param(struct atomisp_sub_device * asd,int flag,struct atomisp_de_config * config)3403 int atomisp_false_color_param(struct atomisp_sub_device *asd, int flag,
3404 			      struct atomisp_de_config *config)
3405 {
3406 	if (flag == 0) {
3407 		/* Get false color from current setup */
3408 		if (atomisp_css_get_de_config(asd, config))
3409 			return -EINVAL;
3410 	} else {
3411 		/* Set false color to isp parameters */
3412 		memcpy(&asd->params.css_param.de_config, config,
3413 		       sizeof(asd->params.css_param.de_config));
3414 		asd->params.config.de_config = &asd->params.css_param.de_config;
3415 		asd->params.css_update_params_needed = true;
3416 	}
3417 
3418 	return 0;
3419 }
3420 
3421 /*
3422  * Function to configure white balance params
3423  */
atomisp_white_balance_param(struct atomisp_sub_device * asd,int flag,struct atomisp_wb_config * config)3424 int atomisp_white_balance_param(struct atomisp_sub_device *asd, int flag,
3425 				struct atomisp_wb_config *config)
3426 {
3427 	if (flag == 0) {
3428 		/* Get white balance from current setup */
3429 		if (atomisp_css_get_wb_config(asd, config))
3430 			return -EINVAL;
3431 	} else {
3432 		/* Set white balance to isp parameters */
3433 		memcpy(&asd->params.css_param.wb_config, config,
3434 		       sizeof(asd->params.css_param.wb_config));
3435 		asd->params.config.wb_config = &asd->params.css_param.wb_config;
3436 		asd->params.css_update_params_needed = true;
3437 	}
3438 
3439 	return 0;
3440 }
3441 
atomisp_3a_config_param(struct atomisp_sub_device * asd,int flag,struct atomisp_3a_config * config)3442 int atomisp_3a_config_param(struct atomisp_sub_device *asd, int flag,
3443 			    struct atomisp_3a_config *config)
3444 {
3445 	struct atomisp_device *isp = asd->isp;
3446 
3447 	dev_dbg(isp->dev, ">%s %d\n", __func__, flag);
3448 
3449 	if (flag == 0) {
3450 		/* Get white balance from current setup */
3451 		if (atomisp_css_get_3a_config(asd, config))
3452 			return -EINVAL;
3453 	} else {
3454 		/* Set white balance to isp parameters */
3455 		memcpy(&asd->params.css_param.s3a_config, config,
3456 		       sizeof(asd->params.css_param.s3a_config));
3457 		asd->params.config.s3a_config = &asd->params.css_param.s3a_config;
3458 		asd->params.css_update_params_needed = true;
3459 	}
3460 
3461 	dev_dbg(isp->dev, "<%s %d\n", __func__, flag);
3462 	return 0;
3463 }
3464 
3465 /*
3466  * Function to setup digital zoom
3467  */
atomisp_digital_zoom(struct atomisp_sub_device * asd,int flag,__s32 * value)3468 int atomisp_digital_zoom(struct atomisp_sub_device *asd, int flag,
3469 			 __s32 *value)
3470 {
3471 	u32 zoom;
3472 	struct atomisp_device *isp = asd->isp;
3473 
3474 	unsigned int max_zoom = MRFLD_MAX_ZOOM_FACTOR;
3475 
3476 	if (flag == 0) {
3477 		atomisp_css_get_zoom_factor(asd, &zoom);
3478 		*value = max_zoom - zoom;
3479 	} else {
3480 		if (*value < 0)
3481 			return -EINVAL;
3482 
3483 		zoom = max_zoom - min_t(u32, max_zoom - 1, *value);
3484 		atomisp_css_set_zoom_factor(asd, zoom);
3485 
3486 		dev_dbg(isp->dev, "%s, zoom: %d\n", __func__, zoom);
3487 		asd->params.css_update_params_needed = true;
3488 	}
3489 
3490 	return 0;
3491 }
3492 
__atomisp_update_stream_env(struct atomisp_sub_device * asd,u16 stream_index,struct atomisp_input_stream_info * stream_info)3493 static void __atomisp_update_stream_env(struct atomisp_sub_device *asd,
3494 					u16 stream_index, struct atomisp_input_stream_info *stream_info)
3495 {
3496 	int i;
3497 
3498 	/* assign virtual channel id return from sensor driver query */
3499 	asd->stream_env[stream_index].ch_id = stream_info->ch_id;
3500 	asd->stream_env[stream_index].isys_configs = stream_info->isys_configs;
3501 	for (i = 0; i < stream_info->isys_configs; i++) {
3502 		asd->stream_env[stream_index].isys_info[i].input_format =
3503 		    stream_info->isys_info[i].input_format;
3504 		asd->stream_env[stream_index].isys_info[i].width =
3505 		    stream_info->isys_info[i].width;
3506 		asd->stream_env[stream_index].isys_info[i].height =
3507 		    stream_info->isys_info[i].height;
3508 	}
3509 }
3510 
__atomisp_init_stream_info(u16 stream_index,struct atomisp_input_stream_info * stream_info)3511 static void __atomisp_init_stream_info(u16 stream_index,
3512 				       struct atomisp_input_stream_info *stream_info)
3513 {
3514 	int i;
3515 
3516 	stream_info->enable = 1;
3517 	stream_info->stream = stream_index;
3518 	stream_info->ch_id = 0;
3519 	stream_info->isys_configs = 0;
3520 	for (i = 0; i < MAX_STREAMS_PER_CHANNEL; i++) {
3521 		stream_info->isys_info[i].input_format = 0;
3522 		stream_info->isys_info[i].width = 0;
3523 		stream_info->isys_info[i].height = 0;
3524 	}
3525 }
3526 
atomisp_fill_pix_format(struct v4l2_pix_format * f,u32 width,u32 height,const struct atomisp_format_bridge * br_fmt)3527 static void atomisp_fill_pix_format(struct v4l2_pix_format *f,
3528 				    u32 width, u32 height,
3529 				    const struct atomisp_format_bridge *br_fmt)
3530 {
3531 	u32 bytes;
3532 
3533 	f->width = width;
3534 	f->height = height;
3535 	f->pixelformat = br_fmt->pixelformat;
3536 
3537 	/* Adding padding to width for bytesperline calculation */
3538 	width = ia_css_frame_pad_width(width, br_fmt->sh_fmt);
3539 	bytes = BITS_TO_BYTES(br_fmt->depth * width);
3540 
3541 	if (br_fmt->planar)
3542 		f->bytesperline = width;
3543 	else
3544 		f->bytesperline = bytes;
3545 
3546 	f->sizeimage = PAGE_ALIGN(height * bytes);
3547 
3548 	if (f->field == V4L2_FIELD_ANY)
3549 		f->field = V4L2_FIELD_NONE;
3550 
3551 	/*
3552 	 * FIXME: do we need to set this up differently, depending on the
3553 	 * sensor or the pipeline?
3554 	 */
3555 	f->colorspace = V4L2_COLORSPACE_REC709;
3556 	f->ycbcr_enc = V4L2_YCBCR_ENC_709;
3557 	f->xfer_func = V4L2_XFER_FUNC_709;
3558 }
3559 
3560 /* Get sensor padding values for the non padded width x height resolution */
atomisp_get_padding(struct atomisp_device * isp,u32 width,u32 height,u32 * padding_w,u32 * padding_h)3561 void atomisp_get_padding(struct atomisp_device *isp, u32 width, u32 height,
3562 			 u32 *padding_w, u32 *padding_h)
3563 {
3564 	struct atomisp_input_subdev *input = &isp->inputs[isp->asd.input_curr];
3565 	struct v4l2_rect native_rect = input->native_rect;
3566 	const struct atomisp_in_fmt_conv *fc = NULL;
3567 	u32 min_pad_w = ISP2400_MIN_PAD_W;
3568 	u32 min_pad_h = ISP2400_MIN_PAD_H;
3569 	struct v4l2_mbus_framefmt *sink;
3570 
3571 	if (!input->crop_support) {
3572 		*padding_w = pad_w;
3573 		*padding_h = pad_h;
3574 		return;
3575 	}
3576 
3577 	width = min(width, input->active_rect.width);
3578 	height = min(height, input->active_rect.height);
3579 
3580 	if (input->binning_support && width <= (input->active_rect.width / 2) &&
3581 				      height <= (input->active_rect.height / 2)) {
3582 		native_rect.width /= 2;
3583 		native_rect.height /= 2;
3584 	}
3585 
3586 	*padding_w = min_t(u32, (native_rect.width - width) & ~1, pad_w);
3587 	*padding_h = min_t(u32, (native_rect.height - height) & ~1, pad_h);
3588 
3589 	/* The below minimum padding requirements are for BYT / ISP2400 only */
3590 	if (IS_ISP2401)
3591 		return;
3592 
3593 	sink = atomisp_subdev_get_ffmt(&isp->asd.subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
3594 				       ATOMISP_SUBDEV_PAD_SINK);
3595 	if (sink)
3596 		fc = atomisp_find_in_fmt_conv(sink->code);
3597 	if (!fc) {
3598 		dev_warn(isp->dev, "%s: Could not get sensor format\n", __func__);
3599 		goto apply_min_padding;
3600 	}
3601 
3602 	/*
3603 	 * The ISP only supports GRBG for other bayer-orders additional padding
3604 	 * is used so that the raw sensor data can be cropped to fix the order.
3605 	 */
3606 	if (fc->bayer_order == IA_CSS_BAYER_ORDER_RGGB ||
3607 	    fc->bayer_order == IA_CSS_BAYER_ORDER_GBRG)
3608 		min_pad_w += 2;
3609 
3610 	if (fc->bayer_order == IA_CSS_BAYER_ORDER_BGGR ||
3611 	    fc->bayer_order == IA_CSS_BAYER_ORDER_GBRG)
3612 		min_pad_h += 2;
3613 
3614 apply_min_padding:
3615 	*padding_w = max_t(u32, *padding_w, min_pad_w);
3616 	*padding_h = max_t(u32, *padding_h, min_pad_h);
3617 }
3618 
atomisp_s_sensor_power(struct atomisp_device * isp,unsigned int input,bool on)3619 int atomisp_s_sensor_power(struct atomisp_device *isp, unsigned int input, bool on)
3620 {
3621 	int ret;
3622 
3623 	if (isp->inputs[input].camera_on == on)
3624 		return 0;
3625 
3626 	ret = v4l2_subdev_call(isp->inputs[input].camera, core, s_power, on);
3627 	if (ret && ret != -ENOIOCTLCMD) {
3628 		dev_err(isp->dev, "Error setting sensor power %d: %d\n", on, ret);
3629 		return ret;
3630 	}
3631 
3632 	isp->inputs[input].camera_on = on;
3633 	return 0;
3634 }
3635 
atomisp_select_input(struct atomisp_device * isp,unsigned int input)3636 int atomisp_select_input(struct atomisp_device *isp, unsigned int input)
3637 {
3638 	unsigned int input_orig = isp->asd.input_curr;
3639 	int ret;
3640 
3641 	/* Power on new sensor */
3642 	ret = atomisp_s_sensor_power(isp, input, 1);
3643 	if (ret)
3644 		return ret;
3645 
3646 	isp->asd.input_curr = input;
3647 
3648 	/* Power off previous sensor */
3649 	if (input != input_orig)
3650 		atomisp_s_sensor_power(isp, input_orig, 0);
3651 
3652 	atomisp_setup_input_links(isp);
3653 	return 0;
3654 }
3655 
3656 /*
3657  * Ensure the CSI-receiver -> ISP link for input_curr is marked as enabled and
3658  * the other CSI-receiver -> ISP links are disabled.
3659  */
atomisp_setup_input_links(struct atomisp_device * isp)3660 void atomisp_setup_input_links(struct atomisp_device *isp)
3661 {
3662 	struct media_link *link;
3663 
3664 	lockdep_assert_held(&isp->media_dev.graph_mutex);
3665 
3666 	for (int i = 0; i < ATOMISP_CAMERA_NR_PORTS; i++) {
3667 		link = media_entity_find_link(
3668 				&isp->csi2_port[i].subdev.entity.pads[CSI2_PAD_SOURCE],
3669 				&isp->asd.subdev.entity.pads[ATOMISP_SUBDEV_PAD_SINK]);
3670 		if (!link) {
3671 			dev_err(isp->dev, "Error cannot find CSI2-port[%d] -> ISP link\n", i);
3672 			continue; /* Should never happen */
3673 		}
3674 
3675 		/*
3676 		 * Modify the flags directly, calling media_entity_setup_link()
3677 		 * will end up calling atomisp_link_setup() which calls this
3678 		 * function again leading to endless recursion.
3679 		 */
3680 		if (isp->sensor_subdevs[i] == isp->inputs[isp->asd.input_curr].camera)
3681 			link->flags |= MEDIA_LNK_FL_ENABLED;
3682 		else
3683 			link->flags &= ~MEDIA_LNK_FL_ENABLED;
3684 
3685 		link->reverse->flags = link->flags;
3686 	}
3687 }
3688 
atomisp_set_sensor_crop_and_fmt(struct atomisp_device * isp,struct v4l2_mbus_framefmt * ffmt,int which)3689 static int atomisp_set_sensor_crop_and_fmt(struct atomisp_device *isp,
3690 					   struct v4l2_mbus_framefmt *ffmt,
3691 					   int which)
3692 {
3693 	struct atomisp_input_subdev *input = &isp->inputs[isp->asd.input_curr];
3694 	struct v4l2_subdev_selection sel = {
3695 		.which = which,
3696 		.target = V4L2_SEL_TGT_CROP,
3697 		.r.width = ffmt->width,
3698 		.r.height = ffmt->height,
3699 	};
3700 	struct v4l2_subdev_format format = {
3701 		.which = which,
3702 		.format = *ffmt,
3703 	};
3704 	struct v4l2_subdev_state *sd_state;
3705 	int ret = 0;
3706 
3707 	if (!input->camera)
3708 		return -EINVAL;
3709 
3710 	/*
3711 	 * Some old sensor drivers already write the registers on set_fmt
3712 	 * instead of on stream on, power on the sensor now (on newer
3713 	 * sensor drivers the s_power op is a no-op).
3714 	 */
3715 	if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
3716 		ret = atomisp_s_sensor_power(isp, isp->asd.input_curr, 1);
3717 		if (ret)
3718 			return ret;
3719 	}
3720 
3721 	sd_state = (which == V4L2_SUBDEV_FORMAT_TRY) ? input->try_sd_state :
3722 						       input->camera->active_state;
3723 	if (sd_state)
3724 		v4l2_subdev_lock_state(sd_state);
3725 
3726 	if (!input->crop_support)
3727 		goto set_fmt;
3728 
3729 	/* Cropping is done before binning, when binning double the crop rect */
3730 	if (input->binning_support && sel.r.width <= (input->native_rect.width / 2) &&
3731 				      sel.r.height <= (input->native_rect.height / 2)) {
3732 		sel.r.width *= 2;
3733 		sel.r.height *= 2;
3734 	}
3735 
3736 	/* Clamp to avoid top/left calculations overflowing */
3737 	sel.r.width = min(sel.r.width, input->native_rect.width);
3738 	sel.r.height = min(sel.r.height, input->native_rect.height);
3739 
3740 	sel.r.left = ((input->native_rect.width - sel.r.width) / 2) & ~1;
3741 	sel.r.top = ((input->native_rect.height - sel.r.height) / 2) & ~1;
3742 
3743 	ret = v4l2_subdev_call(input->camera, pad, set_selection, sd_state, &sel);
3744 	if (ret)
3745 		dev_err(isp->dev, "Error setting crop to %ux%u @%ux%u: %d\n",
3746 			sel.r.width, sel.r.height, sel.r.left, sel.r.top, ret);
3747 
3748 set_fmt:
3749 	if (ret == 0)
3750 		ret = v4l2_subdev_call(input->camera, pad, set_fmt, sd_state, &format);
3751 
3752 	if (sd_state)
3753 		v4l2_subdev_unlock_state(sd_state);
3754 
3755 	/* Propagate new fmt to CSI port */
3756 	if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
3757 		ret = v4l2_subdev_call(input->csi_port, pad, set_fmt, NULL, &format);
3758 		if (ret)
3759 			return ret;
3760 	}
3761 
3762 	*ffmt = format.format;
3763 	return ret;
3764 }
3765 
3766 /* This function looks up the closest available resolution. */
atomisp_try_fmt(struct atomisp_device * isp,struct v4l2_pix_format * f,const struct atomisp_format_bridge ** fmt_ret,const struct atomisp_format_bridge ** snr_fmt_ret)3767 int atomisp_try_fmt(struct atomisp_device *isp, struct v4l2_pix_format *f,
3768 		    const struct atomisp_format_bridge **fmt_ret,
3769 		    const struct atomisp_format_bridge **snr_fmt_ret)
3770 {
3771 	const struct atomisp_format_bridge *fmt, *snr_fmt;
3772 	struct atomisp_sub_device *asd = &isp->asd;
3773 	struct v4l2_mbus_framefmt ffmt = { };
3774 	u32 padding_w, padding_h;
3775 	int ret;
3776 
3777 	fmt = atomisp_get_format_bridge(f->pixelformat);
3778 	/* Currently, raw formats are broken!!! */
3779 	if (!fmt || fmt->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
3780 		f->pixelformat = V4L2_PIX_FMT_YUV420;
3781 
3782 		fmt = atomisp_get_format_bridge(f->pixelformat);
3783 		if (!fmt)
3784 			return -EINVAL;
3785 	}
3786 
3787 	/* The preview pipeline does not support width > 1920 */
3788 	if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW)
3789 		f->width = min_t(u32, f->width, 1920);
3790 
3791 	/*
3792 	 * atomisp_set_fmt() will set the sensor resolution to the requested
3793 	 * resolution + padding. Add padding here and remove it again after
3794 	 * the set_fmt call, like atomisp_set_fmt_to_snr() does.
3795 	 */
3796 	atomisp_get_padding(isp, f->width, f->height, &padding_w, &padding_h);
3797 	v4l2_fill_mbus_format(&ffmt, f, fmt->mbus_code);
3798 	ffmt.width += padding_w;
3799 	ffmt.height += padding_h;
3800 
3801 	dev_dbg(isp->dev, "try_mbus_fmt: try %ux%u\n", ffmt.width, ffmt.height);
3802 
3803 	ret = atomisp_set_sensor_crop_and_fmt(isp, &ffmt, V4L2_SUBDEV_FORMAT_TRY);
3804 	if (ret)
3805 		return ret;
3806 
3807 	dev_dbg(isp->dev, "try_mbus_fmt: got %ux%u\n", ffmt.width, ffmt.height);
3808 
3809 	snr_fmt = atomisp_get_format_bridge_from_mbus(ffmt.code);
3810 	if (!snr_fmt) {
3811 		dev_err(isp->dev, "unknown sensor format 0x%8.8x\n",
3812 			ffmt.code);
3813 		return -EINVAL;
3814 	}
3815 
3816 	f->width = ffmt.width - padding_w;
3817 	f->height = ffmt.height - padding_h;
3818 
3819 	/*
3820 	 * If the format is jpeg or custom RAW, then the width and height will
3821 	 * not satisfy the normal atomisp requirements and no need to check
3822 	 * the below conditions. So just assign to what is being returned from
3823 	 * the sensor driver.
3824 	 */
3825 	if (f->pixelformat == V4L2_PIX_FMT_JPEG ||
3826 	    f->pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW)
3827 		goto out_fill_pix_format;
3828 
3829 	/* app vs isp */
3830 	f->width = rounddown(clamp_t(u32, f->width, ATOM_ISP_MIN_WIDTH,
3831 				     ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
3832 	f->height = rounddown(clamp_t(u32, f->height, ATOM_ISP_MIN_HEIGHT,
3833 				      ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
3834 
3835 out_fill_pix_format:
3836 	atomisp_fill_pix_format(f, f->width, f->height, fmt);
3837 
3838 	if (fmt_ret)
3839 		*fmt_ret = fmt;
3840 
3841 	if (snr_fmt_ret)
3842 		*snr_fmt_ret = snr_fmt;
3843 
3844 	return 0;
3845 }
3846 
atomisp_port_to_mipi_port(struct atomisp_device * isp,enum atomisp_camera_port port)3847 enum mipi_port_id atomisp_port_to_mipi_port(struct atomisp_device *isp,
3848 					    enum atomisp_camera_port port)
3849 {
3850 	switch (port) {
3851 	case ATOMISP_CAMERA_PORT_PRIMARY:
3852 		return MIPI_PORT0_ID;
3853 	case ATOMISP_CAMERA_PORT_SECONDARY:
3854 		return MIPI_PORT1_ID;
3855 	case ATOMISP_CAMERA_PORT_TERTIARY:
3856 		return MIPI_PORT2_ID;
3857 	default:
3858 		dev_err(isp->dev, "unsupported port: %d\n", port);
3859 		return MIPI_PORT0_ID;
3860 	}
3861 }
3862 
atomisp_set_sensor_mipi_to_isp(struct atomisp_sub_device * asd,enum atomisp_input_stream_id stream_id,struct camera_mipi_info * mipi_info)3863 static inline int atomisp_set_sensor_mipi_to_isp(
3864     struct atomisp_sub_device *asd,
3865     enum atomisp_input_stream_id stream_id,
3866     struct camera_mipi_info *mipi_info)
3867 {
3868 	struct v4l2_control ctrl;
3869 	struct atomisp_device *isp = asd->isp;
3870 	struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr];
3871 	const struct atomisp_in_fmt_conv *fc;
3872 	int mipi_freq = 0;
3873 	unsigned int input_format, bayer_order;
3874 	enum atomisp_input_format metadata_format = ATOMISP_INPUT_FORMAT_EMBEDDED;
3875 	u32 mipi_port, metadata_width = 0, metadata_height = 0;
3876 
3877 	ctrl.id = V4L2_CID_LINK_FREQ;
3878 	if (v4l2_g_ctrl(input->camera->ctrl_handler, &ctrl) == 0)
3879 		mipi_freq = ctrl.value;
3880 
3881 	if (asd->stream_env[stream_id].isys_configs == 1) {
3882 		input_format =
3883 		    asd->stream_env[stream_id].isys_info[0].input_format;
3884 		atomisp_css_isys_set_format(asd, stream_id,
3885 					    input_format, IA_CSS_STREAM_DEFAULT_ISYS_STREAM_IDX);
3886 	} else if (asd->stream_env[stream_id].isys_configs == 2) {
3887 		atomisp_css_isys_two_stream_cfg_update_stream1(
3888 		    asd, stream_id,
3889 		    asd->stream_env[stream_id].isys_info[0].input_format,
3890 		    asd->stream_env[stream_id].isys_info[0].width,
3891 		    asd->stream_env[stream_id].isys_info[0].height);
3892 
3893 		atomisp_css_isys_two_stream_cfg_update_stream2(
3894 		    asd, stream_id,
3895 		    asd->stream_env[stream_id].isys_info[1].input_format,
3896 		    asd->stream_env[stream_id].isys_info[1].width,
3897 		    asd->stream_env[stream_id].isys_info[1].height);
3898 	}
3899 
3900 	/* Compatibility for sensors which provide no media bus code
3901 	 * in s_mbus_framefmt() nor support pad formats. */
3902 	if (mipi_info && mipi_info->input_format != -1) {
3903 		bayer_order = mipi_info->raw_bayer_order;
3904 
3905 		/* Input stream config is still needs configured */
3906 		/* TODO: Check if this is necessary */
3907 		fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
3908 			 mipi_info->input_format);
3909 		if (!fc)
3910 			return -EINVAL;
3911 		input_format = fc->atomisp_in_fmt;
3912 		metadata_format = mipi_info->metadata_format;
3913 		metadata_width = mipi_info->metadata_width;
3914 		metadata_height = mipi_info->metadata_height;
3915 	} else {
3916 		struct v4l2_mbus_framefmt *sink;
3917 
3918 		sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
3919 					       V4L2_SUBDEV_FORMAT_ACTIVE,
3920 					       ATOMISP_SUBDEV_PAD_SINK);
3921 		fc = atomisp_find_in_fmt_conv(sink->code);
3922 		if (!fc)
3923 			return -EINVAL;
3924 		input_format = fc->atomisp_in_fmt;
3925 		bayer_order = fc->bayer_order;
3926 	}
3927 
3928 	atomisp_css_input_set_format(asd, stream_id, input_format);
3929 	atomisp_css_input_set_bayer_order(asd, stream_id, bayer_order);
3930 
3931 	fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(metadata_format);
3932 	if (!fc)
3933 		return -EINVAL;
3934 
3935 	input_format = fc->atomisp_in_fmt;
3936 	mipi_port = atomisp_port_to_mipi_port(isp, input->port);
3937 	atomisp_css_input_configure_port(asd, mipi_port,
3938 					 isp->sensor_lanes[mipi_port],
3939 					 0xffff4, mipi_freq,
3940 					 input_format,
3941 					 metadata_width, metadata_height);
3942 	return 0;
3943 }
3944 
configure_pp_input_nop(struct atomisp_sub_device * asd,unsigned int width,unsigned int height)3945 static int configure_pp_input_nop(struct atomisp_sub_device *asd,
3946 				  unsigned int width, unsigned int height)
3947 {
3948 	return 0;
3949 }
3950 
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)3951 static int configure_output_nop(struct atomisp_sub_device *asd,
3952 				unsigned int width, unsigned int height,
3953 				unsigned int min_width,
3954 				enum ia_css_frame_format sh_fmt)
3955 {
3956 	return 0;
3957 }
3958 
get_frame_info_nop(struct atomisp_sub_device * asd,struct ia_css_frame_info * finfo)3959 static int get_frame_info_nop(struct atomisp_sub_device *asd,
3960 			      struct ia_css_frame_info *finfo)
3961 {
3962 	return 0;
3963 }
3964 
3965 /*
3966  * Resets CSS parameters that depend on input resolution.
3967  *
3968  * Update params like CSS RAW binning, 2ppc mode and pp_input
3969  * which depend on input size, but are not automatically
3970  * handled in CSS when the input resolution is changed.
3971  */
css_input_resolution_changed(struct atomisp_sub_device * asd,struct v4l2_mbus_framefmt * ffmt)3972 static int css_input_resolution_changed(struct atomisp_sub_device *asd,
3973 					struct v4l2_mbus_framefmt *ffmt)
3974 {
3975 	struct atomisp_metadata_buf *md_buf = NULL, *_md_buf;
3976 	unsigned int i;
3977 
3978 	dev_dbg(asd->isp->dev, "css_input_resolution_changed to %ux%u\n",
3979 		ffmt->width, ffmt->height);
3980 
3981 	if (IS_ISP2401)
3982 		atomisp_css_input_set_two_pixels_per_clock(asd, false);
3983 	else
3984 		atomisp_css_input_set_two_pixels_per_clock(asd, true);
3985 
3986 	/*
3987 	 * If sensor input changed, which means metadata resolution changed
3988 	 * together. Release all metadata buffers here to let it re-allocated
3989 	 * next time in reqbufs.
3990 	 */
3991 	for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
3992 		list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
3993 					 list) {
3994 			atomisp_css_free_metadata_buffer(md_buf);
3995 			list_del(&md_buf->list);
3996 			kfree(md_buf);
3997 		}
3998 	}
3999 	return 0;
4000 
4001 	/*
4002 	 * TODO: atomisp_css_preview_configure_pp_input() not
4003 	 *       reset due to CSS bug tracked as PSI BZ 115124
4004 	 */
4005 }
4006 
atomisp_set_fmt_to_isp(struct video_device * vdev,struct ia_css_frame_info * output_info,const struct v4l2_pix_format * pix)4007 static int atomisp_set_fmt_to_isp(struct video_device *vdev,
4008 				  struct ia_css_frame_info *output_info,
4009 				  const struct v4l2_pix_format *pix)
4010 {
4011 	struct camera_mipi_info *mipi_info;
4012 	struct atomisp_device *isp = video_get_drvdata(vdev);
4013 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
4014 	struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr];
4015 	const struct atomisp_format_bridge *format;
4016 	struct v4l2_rect *isp_sink_crop;
4017 	enum ia_css_pipe_id pipe_id;
4018 	int (*configure_output)(struct atomisp_sub_device *asd,
4019 				unsigned int width, unsigned int height,
4020 				unsigned int min_width,
4021 				enum ia_css_frame_format sh_fmt) =
4022 				    configure_output_nop;
4023 	int (*get_frame_info)(struct atomisp_sub_device *asd,
4024 			      struct ia_css_frame_info *finfo) =
4025 				  get_frame_info_nop;
4026 	int (*configure_pp_input)(struct atomisp_sub_device *asd,
4027 				  unsigned int width, unsigned int height) =
4028 				      configure_pp_input_nop;
4029 	const struct atomisp_in_fmt_conv *fc = NULL;
4030 	struct v4l2_mbus_framefmt *ffmt;
4031 	int ret, i;
4032 
4033 	isp_sink_crop = atomisp_subdev_get_rect(
4034 			    &asd->subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
4035 			    ATOMISP_SUBDEV_PAD_SINK, V4L2_SEL_TGT_CROP);
4036 
4037 	format = atomisp_get_format_bridge(pix->pixelformat);
4038 	if (!format)
4039 		return -EINVAL;
4040 
4041 	mipi_info = atomisp_to_sensor_mipi_info(input->camera);
4042 
4043 	if (atomisp_set_sensor_mipi_to_isp(asd, ATOMISP_INPUT_STREAM_GENERAL,
4044 					   mipi_info))
4045 		return -EINVAL;
4046 
4047 	if (mipi_info)
4048 		fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(mipi_info->input_format);
4049 	if (!fc) {
4050 		ffmt = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4051 					       V4L2_SUBDEV_FORMAT_ACTIVE,
4052 					       ATOMISP_SUBDEV_PAD_SINK);
4053 		fc = atomisp_find_in_fmt_conv(ffmt->code);
4054 	}
4055 	if (!fc)
4056 		return -EINVAL;
4057 
4058 	if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW &&
4059 	    raw_output_format_match_input(fc->atomisp_in_fmt, pix->pixelformat))
4060 		return -EINVAL;
4061 
4062 	/*
4063 	 * Configure viewfinder also when vfpp is disabled: the
4064 	 * CSS still requires viewfinder configuration.
4065 	 */
4066 	{
4067 		u32 width, height;
4068 
4069 		if (pix->width < 640 || pix->height < 480) {
4070 			width = pix->width;
4071 			height = pix->height;
4072 		} else {
4073 			width = 640;
4074 			height = 480;
4075 		}
4076 
4077 		if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO ||
4078 		    asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
4079 			atomisp_css_video_configure_viewfinder(asd, width, height, 0,
4080 							       IA_CSS_FRAME_FORMAT_NV12);
4081 		} else if (asd->run_mode->val == ATOMISP_RUN_MODE_STILL_CAPTURE ||
4082 			   asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
4083 			atomisp_css_capture_configure_viewfinder(asd, width, height, 0,
4084 								 IA_CSS_FRAME_FORMAT_NV12);
4085 		}
4086 	}
4087 
4088 	atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
4089 
4090 	for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++)
4091 		asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipe_extra_configs[i].disable_vf_pp = asd->vfpp->val != ATOMISP_VFPP_ENABLE;
4092 
4093 	/* ISP2401 new input system need to use copy pipe */
4094 	if (asd->copy_mode) {
4095 		pipe_id = IA_CSS_PIPE_ID_COPY;
4096 		atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL, false);
4097 	} else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
4098 		/* video same in continuouscapture and online modes */
4099 		configure_output = atomisp_css_video_configure_output;
4100 		get_frame_info = atomisp_css_video_get_output_frame_info;
4101 		pipe_id = IA_CSS_PIPE_ID_VIDEO;
4102 	} else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4103 		configure_output = atomisp_css_video_configure_output;
4104 		get_frame_info = atomisp_css_video_get_output_frame_info;
4105 		pipe_id = IA_CSS_PIPE_ID_VIDEO;
4106 	} else if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW) {
4107 		configure_output = atomisp_css_preview_configure_output;
4108 		get_frame_info = atomisp_css_preview_get_output_frame_info;
4109 		configure_pp_input = atomisp_css_preview_configure_pp_input;
4110 		pipe_id = IA_CSS_PIPE_ID_PREVIEW;
4111 	} else {
4112 		if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
4113 			atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
4114 			atomisp_css_enable_dz(asd, false);
4115 		} else {
4116 			atomisp_update_capture_mode(asd);
4117 		}
4118 
4119 		/* in case of ANR, force capture pipe to offline mode */
4120 		atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
4121 						  !asd->params.low_light);
4122 
4123 		configure_output = atomisp_css_capture_configure_output;
4124 		get_frame_info = atomisp_css_capture_get_output_frame_info;
4125 		configure_pp_input = atomisp_css_capture_configure_pp_input;
4126 		pipe_id = IA_CSS_PIPE_ID_CAPTURE;
4127 
4128 		if (asd->run_mode->val != ATOMISP_RUN_MODE_STILL_CAPTURE) {
4129 			dev_err(isp->dev,
4130 				"Need to set the running mode first\n");
4131 			asd->run_mode->val = ATOMISP_RUN_MODE_STILL_CAPTURE;
4132 		}
4133 	}
4134 
4135 	if (asd->copy_mode)
4136 		ret = atomisp_css_copy_configure_output(asd, ATOMISP_INPUT_STREAM_GENERAL,
4137 							pix->width, pix->height,
4138 							format->planar ? pix->bytesperline :
4139 							pix->bytesperline * 8 / format->depth,
4140 							format->sh_fmt);
4141 	else
4142 		ret = configure_output(asd, pix->width, pix->height,
4143 				       format->planar ? pix->bytesperline :
4144 				       pix->bytesperline * 8 / format->depth,
4145 				       format->sh_fmt);
4146 	if (ret) {
4147 		dev_err(isp->dev, "configure_output %ux%u, format %8.8x\n",
4148 			pix->width, pix->height, format->sh_fmt);
4149 		return -EINVAL;
4150 	}
4151 
4152 	ret = configure_pp_input(asd, isp_sink_crop->width, isp_sink_crop->height);
4153 	if (ret) {
4154 		dev_err(isp->dev, "configure_pp_input %ux%u\n",
4155 			isp_sink_crop->width,
4156 			isp_sink_crop->height);
4157 		return -EINVAL;
4158 	}
4159 	if (asd->copy_mode)
4160 		ret = atomisp_css_copy_get_output_frame_info(asd,
4161 							     ATOMISP_INPUT_STREAM_GENERAL,
4162 							     output_info);
4163 	else
4164 		ret = get_frame_info(asd, output_info);
4165 	if (ret) {
4166 		dev_err(isp->dev, "__get_frame_info %ux%u (padded to %u) returned %d\n",
4167 			pix->width, pix->height, pix->bytesperline, ret);
4168 		return ret;
4169 	}
4170 
4171 	atomisp_update_grid_info(asd, pipe_id);
4172 	return 0;
4173 }
4174 
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)4175 static void atomisp_get_dis_envelop(struct atomisp_sub_device *asd,
4176 				    unsigned int width, unsigned int height,
4177 				    unsigned int *dvs_env_w, unsigned int *dvs_env_h)
4178 {
4179 	if (asd->params.video_dis_en &&
4180 	    asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4181 		/* envelope is 20% of the output resolution */
4182 		/*
4183 		 * dvs envelope cannot be round up.
4184 		 * it would cause ISP timeout and color switch issue
4185 		 */
4186 		*dvs_env_w = rounddown(width / 5, ATOM_ISP_STEP_WIDTH);
4187 		*dvs_env_h = rounddown(height / 5, ATOM_ISP_STEP_HEIGHT);
4188 	}
4189 
4190 	asd->params.dis_proj_data_valid = false;
4191 	asd->params.css_update_params_needed = true;
4192 }
4193 
atomisp_check_copy_mode(struct atomisp_sub_device * asd,const struct v4l2_pix_format * f)4194 static void atomisp_check_copy_mode(struct atomisp_sub_device *asd,
4195 				    const struct v4l2_pix_format *f)
4196 {
4197 	struct v4l2_mbus_framefmt *sink, *src;
4198 
4199 	if (!IS_ISP2401) {
4200 		/* Only used for the new input system */
4201 		asd->copy_mode = false;
4202 		return;
4203 	}
4204 
4205 	sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4206 				       V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SINK);
4207 	src = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4208 				      V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SOURCE);
4209 
4210 	if (sink->code == src->code && sink->width == f->width && sink->height == f->height)
4211 		asd->copy_mode = true;
4212 	else
4213 		asd->copy_mode = false;
4214 
4215 	dev_dbg(asd->isp->dev, "copy_mode: %d\n", asd->copy_mode);
4216 }
4217 
atomisp_set_fmt_to_snr(struct video_device * vdev,const struct v4l2_pix_format * f,unsigned int dvs_env_w,unsigned int dvs_env_h)4218 static int atomisp_set_fmt_to_snr(struct video_device *vdev, const struct v4l2_pix_format *f,
4219 				  unsigned int dvs_env_w, unsigned int dvs_env_h)
4220 {
4221 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4222 	struct atomisp_sub_device *asd = pipe->asd;
4223 	struct atomisp_device *isp = asd->isp;
4224 	const struct atomisp_format_bridge *format;
4225 	struct v4l2_mbus_framefmt req_ffmt, ffmt = { };
4226 	struct atomisp_input_stream_info *stream_info =
4227 	    (struct atomisp_input_stream_info *)&ffmt.reserved;
4228 	int ret;
4229 
4230 	format = atomisp_get_format_bridge(f->pixelformat);
4231 	if (!format)
4232 		return -EINVAL;
4233 
4234 	v4l2_fill_mbus_format(&ffmt, f, format->mbus_code);
4235 	ffmt.height += asd->sink_pad_padding_h + dvs_env_h;
4236 	ffmt.width += asd->sink_pad_padding_w + dvs_env_w;
4237 
4238 	dev_dbg(isp->dev, "s_mbus_fmt: ask %ux%u (padding %ux%u, dvs %ux%u)\n",
4239 		ffmt.width, ffmt.height, asd->sink_pad_padding_w, asd->sink_pad_padding_h,
4240 		dvs_env_w, dvs_env_h);
4241 
4242 	__atomisp_init_stream_info(ATOMISP_INPUT_STREAM_GENERAL, stream_info);
4243 
4244 	req_ffmt = ffmt;
4245 
4246 	/* Disable dvs if resolution can't be supported by sensor */
4247 	if (asd->params.video_dis_en && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4248 		ret = atomisp_set_sensor_crop_and_fmt(isp, &ffmt, V4L2_SUBDEV_FORMAT_TRY);
4249 		if (ret)
4250 			return ret;
4251 
4252 		dev_dbg(isp->dev, "video dis: sensor width: %d, height: %d\n",
4253 			ffmt.width, ffmt.height);
4254 
4255 		if (ffmt.width < req_ffmt.width ||
4256 		    ffmt.height < req_ffmt.height) {
4257 			req_ffmt.height -= dvs_env_h;
4258 			req_ffmt.width -= dvs_env_w;
4259 			ffmt = req_ffmt;
4260 			dev_warn(isp->dev,
4261 				 "can not enable video dis due to sensor limitation.");
4262 			asd->params.video_dis_en = false;
4263 		}
4264 	}
4265 
4266 	ret = atomisp_set_sensor_crop_and_fmt(isp, &ffmt, V4L2_SUBDEV_FORMAT_ACTIVE);
4267 	if (ret)
4268 		return ret;
4269 
4270 	__atomisp_update_stream_env(asd, ATOMISP_INPUT_STREAM_GENERAL, stream_info);
4271 
4272 	dev_dbg(isp->dev, "sensor width: %d, height: %d\n",
4273 		ffmt.width, ffmt.height);
4274 
4275 	if (ffmt.width < ATOM_ISP_STEP_WIDTH ||
4276 	    ffmt.height < ATOM_ISP_STEP_HEIGHT)
4277 		return -EINVAL;
4278 
4279 	if (asd->params.video_dis_en && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
4280 	    (ffmt.width < req_ffmt.width || ffmt.height < req_ffmt.height)) {
4281 		dev_warn(isp->dev,
4282 			 "can not enable video dis due to sensor limitation.");
4283 		asd->params.video_dis_en = false;
4284 	}
4285 
4286 	atomisp_subdev_set_ffmt(&asd->subdev, NULL,
4287 				V4L2_SUBDEV_FORMAT_ACTIVE,
4288 				ATOMISP_SUBDEV_PAD_SINK, &ffmt);
4289 
4290 	return css_input_resolution_changed(asd, &ffmt);
4291 }
4292 
atomisp_set_fmt(struct video_device * vdev,struct v4l2_format * f)4293 int atomisp_set_fmt(struct video_device *vdev, struct v4l2_format *f)
4294 {
4295 	struct atomisp_device *isp = video_get_drvdata(vdev);
4296 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4297 	struct atomisp_sub_device *asd = pipe->asd;
4298 	const struct atomisp_format_bridge *format_bridge;
4299 	const struct atomisp_format_bridge *snr_format_bridge;
4300 	struct ia_css_frame_info output_info;
4301 	unsigned int dvs_env_w = 0, dvs_env_h = 0;
4302 	struct v4l2_mbus_framefmt isp_source_fmt = {0};
4303 	struct v4l2_rect isp_sink_crop;
4304 	int ret;
4305 
4306 	ret = atomisp_pipe_check(pipe, true);
4307 	if (ret)
4308 		return ret;
4309 
4310 	dev_dbg(isp->dev,
4311 		"setting resolution %ux%u bytesperline %u\n",
4312 		f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.bytesperline);
4313 
4314 	/* Ensure that the resolution is equal or below the maximum supported */
4315 	ret = atomisp_try_fmt(isp, &f->fmt.pix, &format_bridge, &snr_format_bridge);
4316 	if (ret)
4317 		return ret;
4318 
4319 	pipe->sh_fmt = format_bridge->sh_fmt;
4320 	pipe->pix.pixelformat = format_bridge->pixelformat;
4321 
4322 	atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4323 				V4L2_SUBDEV_FORMAT_ACTIVE,
4324 				ATOMISP_SUBDEV_PAD_SINK)->code =
4325 				    snr_format_bridge->mbus_code;
4326 
4327 	isp_source_fmt.code = format_bridge->mbus_code;
4328 	atomisp_subdev_set_ffmt(&asd->subdev, NULL,
4329 				V4L2_SUBDEV_FORMAT_ACTIVE,
4330 				ATOMISP_SUBDEV_PAD_SOURCE, &isp_source_fmt);
4331 
4332 	if (atomisp_subdev_format_conversion(asd)) {
4333 		atomisp_get_padding(isp, f->fmt.pix.width, f->fmt.pix.height,
4334 				    &asd->sink_pad_padding_w, &asd->sink_pad_padding_h);
4335 	} else {
4336 		asd->sink_pad_padding_w = 0;
4337 		asd->sink_pad_padding_h = 0;
4338 	}
4339 
4340 	atomisp_get_dis_envelop(asd, f->fmt.pix.width, f->fmt.pix.height,
4341 				&dvs_env_w, &dvs_env_h);
4342 
4343 	ret = atomisp_set_fmt_to_snr(vdev, &f->fmt.pix, dvs_env_w, dvs_env_h);
4344 	if (ret) {
4345 		dev_warn(isp->dev,
4346 			 "Set format to sensor failed with %d\n", ret);
4347 		return -EINVAL;
4348 	}
4349 
4350 	atomisp_csi_lane_config(isp);
4351 
4352 	atomisp_check_copy_mode(asd, &f->fmt.pix);
4353 
4354 	isp_sink_crop = *atomisp_subdev_get_rect(&asd->subdev, NULL,
4355 			V4L2_SUBDEV_FORMAT_ACTIVE,
4356 			ATOMISP_SUBDEV_PAD_SINK,
4357 			V4L2_SEL_TGT_CROP);
4358 
4359 	/* Try to enable YUV downscaling if ISP input is 10 % (either
4360 	 * width or height) bigger than the desired result. */
4361 	if (!IS_MOFD ||
4362 	    isp_sink_crop.width * 9 / 10 < f->fmt.pix.width ||
4363 	    isp_sink_crop.height * 9 / 10 < f->fmt.pix.height ||
4364 	    (atomisp_subdev_format_conversion(asd) &&
4365 	     (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO ||
4366 	      asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER))) {
4367 		isp_sink_crop.width = f->fmt.pix.width;
4368 		isp_sink_crop.height = f->fmt.pix.height;
4369 
4370 		atomisp_subdev_set_selection(&asd->subdev, NULL,
4371 					     V4L2_SUBDEV_FORMAT_ACTIVE,
4372 					     ATOMISP_SUBDEV_PAD_SOURCE, V4L2_SEL_TGT_COMPOSE,
4373 					     0, &isp_sink_crop);
4374 	} else {
4375 		struct v4l2_rect main_compose = {0};
4376 
4377 		main_compose.width = isp_sink_crop.width;
4378 		main_compose.height =
4379 		    DIV_ROUND_UP(main_compose.width * f->fmt.pix.height,
4380 				 f->fmt.pix.width);
4381 		if (main_compose.height > isp_sink_crop.height) {
4382 			main_compose.height = isp_sink_crop.height;
4383 			main_compose.width =
4384 			    DIV_ROUND_UP(main_compose.height *
4385 					 f->fmt.pix.width,
4386 					 f->fmt.pix.height);
4387 		}
4388 
4389 		atomisp_subdev_set_selection(&asd->subdev, NULL,
4390 					     V4L2_SUBDEV_FORMAT_ACTIVE,
4391 					     ATOMISP_SUBDEV_PAD_SOURCE,
4392 					     V4L2_SEL_TGT_COMPOSE, 0,
4393 					     &main_compose);
4394 	}
4395 
4396 	ret = atomisp_set_fmt_to_isp(vdev, &output_info, &f->fmt.pix);
4397 	if (ret) {
4398 		dev_warn(isp->dev, "Can't set format on ISP. Error %d\n", ret);
4399 		return -EINVAL;
4400 	}
4401 
4402 	atomisp_fill_pix_format(&pipe->pix, f->fmt.pix.width, f->fmt.pix.height, format_bridge);
4403 
4404 	f->fmt.pix = pipe->pix;
4405 
4406 	dev_dbg(isp->dev, "%s: %dx%d, image size: %d, %d bytes per line\n",
4407 		__func__,
4408 		f->fmt.pix.width, f->fmt.pix.height,
4409 		f->fmt.pix.sizeimage, f->fmt.pix.bytesperline);
4410 
4411 	return 0;
4412 }
4413 
atomisp_set_shading_table(struct atomisp_sub_device * asd,struct atomisp_shading_table * user_shading_table)4414 int atomisp_set_shading_table(struct atomisp_sub_device *asd,
4415 			      struct atomisp_shading_table *user_shading_table)
4416 {
4417 	struct ia_css_shading_table *shading_table;
4418 	struct ia_css_shading_table *free_table;
4419 	unsigned int len_table;
4420 	int i;
4421 	int ret = 0;
4422 
4423 	if (!user_shading_table)
4424 		return -EINVAL;
4425 
4426 	if (!user_shading_table->enable) {
4427 		asd->params.config.shading_table = NULL;
4428 		asd->params.sc_en = false;
4429 		return 0;
4430 	}
4431 
4432 	/* If enabling, all tables must be set */
4433 	for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
4434 		if (!user_shading_table->data[i])
4435 			return -EINVAL;
4436 	}
4437 
4438 	/* Shading table size per color */
4439 	if (user_shading_table->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
4440 	    user_shading_table->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR)
4441 		return -EINVAL;
4442 
4443 	shading_table = atomisp_css_shading_table_alloc(
4444 			    user_shading_table->width, user_shading_table->height);
4445 	if (!shading_table)
4446 		return -ENOMEM;
4447 
4448 	len_table = user_shading_table->width * user_shading_table->height *
4449 		    ATOMISP_SC_TYPE_SIZE;
4450 	for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
4451 		ret = copy_from_user(shading_table->data[i],
4452 				     (void __user *)user_shading_table->data[i],
4453 				     len_table);
4454 		if (ret) {
4455 			free_table = shading_table;
4456 			ret = -EFAULT;
4457 			goto out;
4458 		}
4459 	}
4460 	shading_table->sensor_width = user_shading_table->sensor_width;
4461 	shading_table->sensor_height = user_shading_table->sensor_height;
4462 	shading_table->fraction_bits = user_shading_table->fraction_bits;
4463 
4464 	free_table = asd->params.css_param.shading_table;
4465 	asd->params.css_param.shading_table = shading_table;
4466 	asd->params.config.shading_table = shading_table;
4467 	asd->params.sc_en = true;
4468 
4469 out:
4470 	if (free_table)
4471 		atomisp_css_shading_table_free(free_table);
4472 
4473 	return ret;
4474 }
4475 
__checking_exp_id(struct atomisp_sub_device * asd,int exp_id)4476 static int __checking_exp_id(struct atomisp_sub_device *asd, int exp_id)
4477 {
4478 	struct atomisp_device *isp = asd->isp;
4479 
4480 	if (!asd->enable_raw_buffer_lock->val) {
4481 		dev_warn(isp->dev, "%s Raw Buffer Lock is disable.\n", __func__);
4482 		return -EINVAL;
4483 	}
4484 	if (!asd->streaming) {
4485 		dev_err(isp->dev, "%s streaming %d invalid exp_id %d.\n",
4486 			__func__, exp_id, asd->streaming);
4487 		return -EINVAL;
4488 	}
4489 	if ((exp_id > ATOMISP_MAX_EXP_ID) || (exp_id <= 0)) {
4490 		dev_err(isp->dev, "%s exp_id %d invalid.\n", __func__, exp_id);
4491 		return -EINVAL;
4492 	}
4493 	return 0;
4494 }
4495 
atomisp_init_raw_buffer_bitmap(struct atomisp_sub_device * asd)4496 void atomisp_init_raw_buffer_bitmap(struct atomisp_sub_device *asd)
4497 {
4498 	unsigned long flags;
4499 
4500 	spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
4501 	memset(asd->raw_buffer_bitmap, 0, sizeof(asd->raw_buffer_bitmap));
4502 	asd->raw_buffer_locked_count = 0;
4503 	spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
4504 }
4505 
__is_raw_buffer_locked(struct atomisp_sub_device * asd,int exp_id)4506 static int __is_raw_buffer_locked(struct atomisp_sub_device *asd, int exp_id)
4507 {
4508 	int *bitmap, bit;
4509 	unsigned long flags;
4510 	int ret;
4511 
4512 	if (__checking_exp_id(asd, exp_id))
4513 		return -EINVAL;
4514 
4515 	bitmap = asd->raw_buffer_bitmap + exp_id / 32;
4516 	bit = exp_id % 32;
4517 	spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
4518 	ret = ((*bitmap) & (1 << bit));
4519 	spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
4520 	return !ret;
4521 }
4522 
__clear_raw_buffer_bitmap(struct atomisp_sub_device * asd,int exp_id)4523 static int __clear_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
4524 {
4525 	int *bitmap, bit;
4526 	unsigned long flags;
4527 
4528 	if (__is_raw_buffer_locked(asd, exp_id))
4529 		return -EINVAL;
4530 
4531 	bitmap = asd->raw_buffer_bitmap + exp_id / 32;
4532 	bit = exp_id % 32;
4533 	spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
4534 	(*bitmap) &= ~(1 << bit);
4535 	asd->raw_buffer_locked_count--;
4536 	spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
4537 
4538 	dev_dbg(asd->isp->dev, "%s: exp_id %d,  raw_buffer_locked_count %d\n",
4539 		__func__, exp_id, asd->raw_buffer_locked_count);
4540 	return 0;
4541 }
4542 
atomisp_exp_id_capture(struct atomisp_sub_device * asd,int * exp_id)4543 int atomisp_exp_id_capture(struct atomisp_sub_device *asd, int *exp_id)
4544 {
4545 	struct atomisp_device *isp = asd->isp;
4546 	int value = *exp_id;
4547 	int ret;
4548 
4549 	lockdep_assert_held(&isp->mutex);
4550 
4551 	ret = __is_raw_buffer_locked(asd, value);
4552 	if (ret) {
4553 		dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
4554 		return -EINVAL;
4555 	}
4556 
4557 	dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
4558 	ret = atomisp_css_exp_id_capture(asd, value);
4559 	if (ret) {
4560 		dev_err(isp->dev, "%s exp_id %d failed.\n", __func__, value);
4561 		return -EIO;
4562 	}
4563 	return 0;
4564 }
4565 
atomisp_exp_id_unlock(struct atomisp_sub_device * asd,int * exp_id)4566 int atomisp_exp_id_unlock(struct atomisp_sub_device *asd, int *exp_id)
4567 {
4568 	struct atomisp_device *isp = asd->isp;
4569 	int value = *exp_id;
4570 	int ret;
4571 
4572 	lockdep_assert_held(&isp->mutex);
4573 
4574 	ret = __clear_raw_buffer_bitmap(asd, value);
4575 	if (ret) {
4576 		dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
4577 		return -EINVAL;
4578 	}
4579 
4580 	dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
4581 	ret = atomisp_css_exp_id_unlock(asd, value);
4582 	if (ret)
4583 		dev_err(isp->dev, "%s exp_id %d failed, err %d.\n",
4584 			__func__, value, ret);
4585 
4586 	return ret;
4587 }
4588 
atomisp_enable_dz_capt_pipe(struct atomisp_sub_device * asd,unsigned int * enable)4589 int atomisp_enable_dz_capt_pipe(struct atomisp_sub_device *asd,
4590 				unsigned int *enable)
4591 {
4592 	bool value;
4593 
4594 	if (!enable)
4595 		return -EINVAL;
4596 
4597 	value = *enable > 0;
4598 
4599 	atomisp_en_dz_capt_pipe(asd, value);
4600 
4601 	return 0;
4602 }
4603 
atomisp_inject_a_fake_event(struct atomisp_sub_device * asd,int * event)4604 int atomisp_inject_a_fake_event(struct atomisp_sub_device *asd, int *event)
4605 {
4606 	if (!event || !asd->streaming)
4607 		return -EINVAL;
4608 
4609 	lockdep_assert_held(&asd->isp->mutex);
4610 
4611 	dev_dbg(asd->isp->dev, "%s: trying to inject a fake event 0x%x\n",
4612 		__func__, *event);
4613 
4614 	switch (*event) {
4615 	case V4L2_EVENT_FRAME_SYNC:
4616 		atomisp_sof_event(asd);
4617 		break;
4618 	case V4L2_EVENT_FRAME_END:
4619 		atomisp_eof_event(asd, 0);
4620 		break;
4621 	case V4L2_EVENT_ATOMISP_3A_STATS_READY:
4622 		atomisp_3a_stats_ready_event(asd, 0);
4623 		break;
4624 	case V4L2_EVENT_ATOMISP_METADATA_READY:
4625 		atomisp_metadata_ready_event(asd, 0);
4626 		break;
4627 	default:
4628 		return -EINVAL;
4629 	}
4630 
4631 	return 0;
4632 }
4633