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