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