tizen 2.4 release
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / media / sprd_isp / isp2.0 / tshark2 / src / isp_k_nlm.c
1 /*
2  * Copyright (C) 2012 Spreadtrum Communications Inc.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/uaccess.h>
15 #include <linux/sprd_mm.h>
16 #include <video/sprd_isp.h>
17 #include <linux/vmalloc.h>
18 #include "isp_reg.h"
19
20 static int32_t isp_k_vst_block(struct isp_io_param *param)
21 {
22         int32_t ret = 0;
23         struct isp_dev_vst_info_v1 vst_info;
24
25         ret = copy_from_user((void *)&vst_info, param->property_param, sizeof(vst_info));
26         if (0 != ret) {
27                 printk("isp_k_vst_block: copy error, ret=0x%x\n", (uint32_t)ret);
28                 return -1;
29         }
30
31         REG_MWR(ISP_VST_PARA, BIT_0, vst_info.bypass);
32
33         REG_MWR(ISP_VST_PARA, BIT_1, vst_info.buf_sel << 1);
34
35         return ret;
36
37 }
38
39 static int32_t isp_k_nlm_block(struct isp_io_param *param)
40 {
41         int32_t ret = 0;
42         uint32_t val = 0;
43         uint32_t i = 0;
44         struct isp_dev_nlm_info_v1 nlm_info;
45         uint32_t *buff0 = NULL;
46         uint32_t *buff1 = NULL;
47         uint32_t *buff2 = NULL;
48         unsigned long buf_addr = 0;
49         void *vst_addr = NULL;
50         void *ivst_addr = NULL;
51         void *nlm_addr = NULL;
52
53         ret = copy_from_user((void *)&nlm_info, param->property_param, sizeof(nlm_info));
54         if (0 != ret) {
55                 printk("isp_k_nlm_block: copy error, ret=0x%x\n", (uint32_t)ret);
56                 return -1;
57         }
58
59         buff0 = vzalloc(nlm_info.vst_len);
60         if (NULL == buff0) {
61                 printk("isp_k_nlm_block:vst kmalloc error\n");
62                 return -1;
63         }
64
65         buff1 = vzalloc(nlm_info.ivst_len);
66         if (NULL == buff1) {
67                 printk("isp_k_nlm_block:ivst kmalloc error\n");
68                 vfree(buff0);
69                 return -1;
70         }
71
72         buff2 = vzalloc(nlm_info.nlm_len);
73         if (NULL == buff2) {
74                 printk("isp_k_nlm_block:nlm kmalloc error\n");
75                 vfree(buff0);
76                 vfree(buff1);
77                 return -1;
78         }
79
80 #ifdef CONFIG_64BIT
81         vst_addr = (void*)(((unsigned long)nlm_info.vst_addr[1] << 32) | nlm_info.vst_addr[0]);
82 #else
83         vst_addr = (void*)(nlm_info.vst_addr[0]);
84 #endif
85         ret = copy_from_user((void *)buff0, vst_addr, nlm_info.vst_len);
86         if (0 != ret) {
87                 printk("isp_k_nlm_block: vst copy error, ret=0x%x\n", (uint32_t)ret);
88                 vfree(buff0);
89                 vfree(buff1);
90                 vfree(buff2);
91                 return -1;
92         }
93
94 #ifdef CONFIG_64BIT
95         ivst_addr = (void*)(((unsigned long)nlm_info.ivst_addr[1] << 32) | nlm_info.ivst_addr[0]);
96 #else
97         ivst_addr = (void*)(nlm_info.ivst_addr[0]);
98 #endif
99         ret = copy_from_user((void *)buff1, ivst_addr, nlm_info.ivst_len);
100         if (0 != ret) {
101                 printk("isp_k_nlm_block: ivst copy error, ret=0x%x\n", (uint32_t)ret);
102                 vfree(buff0);
103                 vfree(buff1);
104                 vfree(buff2);
105                 return -1;
106         }
107
108 #ifdef CONFIG_64BIT
109                 nlm_addr = (void*)(((unsigned long)nlm_info.nlm_addr[1] << 32) | nlm_info.nlm_addr[0]);
110 #else
111                 nlm_addr = (void*)(nlm_info.nlm_addr[0]);
112 #endif
113         ret = copy_from_user((void *)buff2, nlm_addr, nlm_info.nlm_len);
114         if (0 != ret) {
115                 printk("isp_k_nlm_block: nlm copy error, ret=0x%x\n", (uint32_t)ret);
116                 vfree(buff0);
117                 vfree(buff1);
118                 vfree(buff2);
119                 return -1;
120         }
121
122         REG_MWR(ISP_NLM_PARA, BIT_1, nlm_info.imp_opt_bypass << 1);
123         REG_MWR(ISP_NLM_PARA, BIT_2, nlm_info.flat_opt_bypass << 2);
124         REG_MWR(ISP_NLM_PARA, BIT_3, nlm_info.flat_thr_bypass << 3);
125         REG_MWR(ISP_NLM_PARA, BIT_4, nlm_info.direction_mode_bypass << 4);
126
127         REG_MWR(ISP_NLM_PARA, BIT_31, nlm_info.buf_sel);
128
129         for (i = 0; i < 5; i++) {
130                 val = (nlm_info.thresh[i] & 0x3FFF) | ((nlm_info.cnt[i] & 0x1F) << 16)
131                         | ((nlm_info.strength[i] & 0xFF) << 24);
132                 REG_WR(ISP_NLM_FLAT_PARA_0 + i * 4, val);
133         }
134
135         REG_MWR(ISP_NLM_STERNGTH, 0x7F, nlm_info.streng_th);
136
137         REG_MWR(ISP_NLM_STERNGTH, 0xFF00, nlm_info.texture_dec << 8);
138
139         //REG_WR(ISP_NLM_IS_FLAT, nlm_info.is_flat & 0xFF);
140
141         REG_MWR(ISP_NLM_ADD_BACK, 0x7F, nlm_info.addback);
142
143         REG_MWR(ISP_NLM_ADD_BACK, BIT_7, nlm_info.opt_mode << 7);
144
145         REG_MWR(ISP_NLM_DIRECTION_0, BIT_17 | BIT_16, nlm_info.dist_mode << 16);
146
147         val = ((nlm_info.w_shift[0] & 0x3) << 4) | ((nlm_info.w_shift[1] & 0x3) << 6) | ((nlm_info.w_shift[2] & 0x3) << 8);
148         REG_MWR(ISP_NLM_DIRECTION_0, 0x3F0, val);
149
150         REG_MWR(ISP_NLM_DIRECTION_0, 0x7, nlm_info.cnt_th);
151
152         REG_MWR(ISP_NLM_DIRECTION_1, 0xFFFF0000, nlm_info.tdist_min_th << 16);
153
154         REG_MWR(ISP_NLM_DIRECTION_1, 0xFFFF, nlm_info.diff_th);
155
156         for (i = 0; i < 24; i++) {
157                 val = (nlm_info.lut_w[i*3+0] & 0x3FF) | ((nlm_info.lut_w[i*3+1] & 0x3FF) << 10)
158                         | ((nlm_info.lut_w[i*3+2] & 0x3FF) << 20);
159                 REG_WR(ISP_NLM_LUT_W_0 + i * 4, val);
160         }
161
162         if(nlm_info.buf_sel) {
163                 buf_addr = ISP_NLM_BUF1_CH0;
164                 memcpy((void *)buf_addr, buff2, ISP_VST_IVST_NUM * sizeof(uint32_t));
165         } else {
166                 buf_addr = ISP_NLM_BUF0_CH0;
167                 memcpy((void *)buf_addr, buff2, ISP_VST_IVST_NUM * sizeof(uint32_t));
168         }
169
170         if (nlm_info.buf_sel ) {
171                 buf_addr = ISP_VST_BUF1_CH0;
172                 memcpy((void *)buf_addr, buff0, ISP_VST_IVST_NUM * sizeof(uint32_t));
173         } else {
174                 buf_addr = ISP_VST_BUF0_CH0;
175                 memcpy((void *)buf_addr, buff0, ISP_VST_IVST_NUM * sizeof(uint32_t));
176         }
177
178         if (nlm_info.buf_sel ) {
179                 buf_addr = ISP_IVST_BUF1_CH0;
180                 memcpy((void *)buf_addr, buff1, ISP_VST_IVST_NUM * sizeof(uint32_t));
181         } else {
182                 buf_addr = ISP_IVST_BUF0_CH0;
183                 memcpy((void *)buf_addr, buff1, ISP_VST_IVST_NUM * sizeof(uint32_t));
184         }
185
186         /*vst ivst follow nlm*/
187         REG_MWR(ISP_VST_PARA, BIT_1, nlm_info.buf_sel << 1);
188         REG_MWR(ISP_IVST_PARA, BIT_1, nlm_info.buf_sel << 1);
189
190         REG_MWR(ISP_NLM_PARA, BIT_0, nlm_info.bypass);
191         REG_MWR(ISP_IVST_PARA, BIT_0, nlm_info.bypass);
192         REG_MWR(ISP_VST_PARA, BIT_0, nlm_info.bypass);
193
194         vfree(buff0);
195         vfree(buff1);
196         vfree(buff2);
197         return ret;
198 }
199
200 static int32_t isp_k_ivst_block(struct isp_io_param *param)
201 {
202         int32_t ret = 0;
203
204         struct isp_dev_ivst_info_v1 ivst_info;
205
206         ret = copy_from_user((void *)&ivst_info, param->property_param, sizeof(ivst_info));
207         if (0 != ret) {
208                 printk("isp_k_ivst_block: copy error, ret=0x%x\n", (uint32_t)ret);
209                 return -1;
210         }
211
212         REG_MWR(ISP_IVST_PARA, BIT_0, ivst_info.bypass);
213
214         REG_MWR(ISP_IVST_PARA, BIT_1, ivst_info.buf_sel << 1);
215
216         return ret;
217
218 }
219
220 int32_t isp_k_cfg_nlm(struct isp_io_param *param)
221 {
222         int32_t ret = 0;
223
224         if (!param) {
225                 printk("isp_k_cfg_nlm: param is null error.\n");
226                 return -1;
227         }
228
229         if (NULL == param->property_param) {
230                 printk("isp_k_cfg_nlm: property_param is null error.\n");
231                 return -1;
232         }
233
234         switch(param->property) {
235         case ISP_PRO_VST_BLOCK:
236                 ret = isp_k_vst_block(param);
237                 break;
238         case ISP_PRO_NLM_BLOCK:
239                 ret = isp_k_nlm_block(param);;
240                 break;
241         case ISP_PRO_IVST_BLOCK:
242                 ret = isp_k_ivst_block(param);;
243                 break;
244         default:
245                 printk("isp_k_cfg_nlm: fail cmd id:%d, not supported.\n", param->property);
246                 break;
247         }
248
249         return ret;
250 }