GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / media / atomisp / pci / atomisp2 / css2400 / isp / kernels / vf / vf_1.0 / ia_css_vf.host.c
1 /*
2  * Support for Intel Camera Imaging ISP subsystem.
3  * Copyright (c) 2015, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  */
14
15 #include "ia_css_vf.host.h"
16 #include <assert_support.h>
17 #include <ia_css_err.h>
18 #include <ia_css_frame.h>
19 #include <ia_css_frame_public.h>
20 #include <ia_css_pipeline.h>
21 #define IA_CSS_INCLUDE_CONFIGURATIONS
22 #include "ia_css_isp_configs.h"
23
24 #include "isp.h"
25
26 void
27 ia_css_vf_config(
28         struct sh_css_isp_vf_isp_config      *to,
29         const struct ia_css_vf_configuration *from,
30         unsigned size)
31 {
32         unsigned elems_a = ISP_VEC_NELEMS;
33
34         (void)size;
35         to->vf_downscale_bits = from->vf_downscale_bits;
36         to->enable = from->info != NULL;
37
38         if (from->info) {
39                 ia_css_frame_info_to_frame_sp_info(&to->info, from->info);
40                 ia_css_dma_configure_from_info(&to->dma.port_b, from->info);
41                 to->dma.width_a_over_b = elems_a / to->dma.port_b.elems;
42
43                 /* Assume divisiblity here, may need to generalize to fixed point. */
44                 assert (elems_a % to->dma.port_b.elems == 0);
45         }
46 }
47
48 /* compute the log2 of the downscale factor needed to get closest
49  * to the requested viewfinder resolution on the upper side. The output cannot
50  * be smaller than the requested viewfinder resolution.
51  */
52 enum ia_css_err
53 sh_css_vf_downscale_log2(
54         const struct ia_css_frame_info *out_info,
55         const struct ia_css_frame_info *vf_info,
56         unsigned int *downscale_log2)
57 {
58        unsigned int ds_log2 = 0;
59        unsigned int out_width;
60
61        if ((out_info == NULL) | (vf_info == NULL))
62                return IA_CSS_ERR_INVALID_ARGUMENTS;
63
64        out_width = out_info->res.width;
65
66        if (out_width == 0)
67                return IA_CSS_ERR_INVALID_ARGUMENTS;
68
69        /* downscale until width smaller than the viewfinder width. We don't
70         * test for the height since the vmem buffers only put restrictions on
71         * the width of a line, not on the number of lines in a frame.
72         */
73        while (out_width >= vf_info->res.width) {
74                ds_log2++;
75                out_width /= 2;
76        }
77        /* now width is smaller, so we go up one step */
78        if ((ds_log2 > 0) && (out_width < ia_css_binary_max_vf_width()))
79                ds_log2--;
80        /* TODO: use actual max input resolution of vf_pp binary */
81        if ((out_info->res.width >> ds_log2) >= 2 * ia_css_binary_max_vf_width())
82                return IA_CSS_ERR_INVALID_ARGUMENTS;
83        *downscale_log2 = ds_log2;
84        return IA_CSS_SUCCESS;
85 }
86
87 static enum ia_css_err
88 configure_kernel(
89         const struct ia_css_binary_info *info,
90         const struct ia_css_frame_info *out_info,
91         const struct ia_css_frame_info *vf_info,
92         unsigned int *downscale_log2,
93         struct ia_css_vf_configuration *config)
94 {
95        enum ia_css_err err;
96        unsigned vf_log_ds = 0;
97
98        /* First compute value */
99        if (vf_info) {
100                err = sh_css_vf_downscale_log2(out_info, vf_info, &vf_log_ds);
101                if (err != IA_CSS_SUCCESS)
102                        return err;
103        }
104        vf_log_ds = min(vf_log_ds, info->vf_dec.max_log_downscale);
105        *downscale_log2 = vf_log_ds;
106
107        /* Then store it in isp config section */
108        config->vf_downscale_bits = vf_log_ds;
109        return IA_CSS_SUCCESS;
110 }
111
112 static void
113 configure_dma(
114         struct ia_css_vf_configuration *config,
115         const struct ia_css_frame_info *vf_info)
116 {
117         config->info = vf_info;
118 }
119
120 enum ia_css_err
121 ia_css_vf_configure(
122         const struct ia_css_binary *binary,
123         const struct ia_css_frame_info *out_info,
124         struct ia_css_frame_info *vf_info,
125         unsigned int *downscale_log2)
126 {
127         enum ia_css_err err;
128         struct ia_css_vf_configuration config;
129         const struct ia_css_binary_info *info = &binary->info->sp;
130
131         err = configure_kernel(info, out_info, vf_info, downscale_log2, &config);
132         configure_dma(&config, vf_info);
133         if (binary) {
134                 if (vf_info)
135                         vf_info->raw_bit_depth = info->dma.vfdec_bits_per_pixel;
136                 ia_css_configure_vf (binary, &config);
137         }
138         return IA_CSS_SUCCESS;
139 }
140