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