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