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