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