OSDN Git Service

Merge "mm-camera2:isp2: Avoid use after free buffer"
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / media / platform / msm / camera_v2 / isp / msm_isp44.c
1 /* Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 and
5  * only version 2 as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 #include <linux/module.h>
14 #include <linux/ratelimit.h>
15
16 #include "msm_isp44.h"
17 #include "msm_isp_util.h"
18 #include "msm_isp_axi_util.h"
19 #include "msm_isp_stats_util.h"
20 #include "msm_isp.h"
21 #include "msm.h"
22 #include "msm_camera_io_util.h"
23 #include "msm_isp47.h"
24 #include "linux/iopoll.h"
25
26 #undef CDBG
27 #define CDBG(fmt, args...) pr_debug(fmt, ##args)
28
29 #define STATS_IDX_BF_SCALE  0
30 #define STATS_IDX_BE        1
31 #define STATS_IDX_BG        2
32 #define STATS_IDX_BF        3
33 #define STATS_IDX_AWB       4
34 #define STATS_IDX_RS        5
35 #define STATS_IDX_CS        6
36 #define STATS_IDX_IHIST     7
37 #define STATS_IDX_BHIST     8
38
39 #define VFE44_8084V1_VERSION   0x4000000A
40
41 #define VFE44_BURST_LEN 3
42 #define VFE44_FETCH_BURST_LEN 3
43 #define VFE44_STATS_BURST_LEN 2
44 #define VFE44_UB_SIZE 2048
45 #define MSM_ISP44_TOTAL_IMAGE_UB 1528
46 #define VFE44_WM_BASE(idx) (0x6C + 0x24 * idx)
47 #define VFE44_RDI_BASE(idx) (0x2E8 + 0x4 * idx)
48 #define VFE44_XBAR_BASE(idx) (0x58 + 0x4 * (idx / 2))
49 #define VFE44_XBAR_SHIFT(idx) ((idx%2) ? 16 : 0)
50 #define VFE44_PING_PONG_BASE(wm, ping_pong) \
51         (VFE44_WM_BASE(wm) + 0x4 * (1 + ((~ping_pong) & 0x1)))
52
53 #define VFE44_BUS_RD_CGC_OVERRIDE_BIT 16
54
55 static uint8_t stats_pingpong_offset_map[] = {
56         7, 8, 9, 10, 11, 12, 13, 14, 15};
57
58 #define SHIFT_BF_SCALE_BIT 1
59 #define VFE44_NUM_STATS_COMP 2
60 #define VFE44_NUM_STATS_TYPE 9
61 #define VFE44_STATS_BASE(idx) \
62         ((idx) == STATS_IDX_BF_SCALE ? 0xA0C : (0x168 + 0x18 * (idx-1)))
63 #define VFE44_STATS_PING_PONG_BASE(idx, ping_pong) \
64         (VFE44_STATS_BASE(idx) + 0x4 * \
65         (~(ping_pong >> (stats_pingpong_offset_map[idx])) & 0x1))
66
67 #define VFE44_CLK_IDX 2
68
69 static uint32_t msm_vfe44_ub_reg_offset(struct vfe_device *vfe_dev, int wm_idx)
70 {
71         return (VFE44_WM_BASE(wm_idx) + 0x10);
72 }
73
74 static uint32_t msm_vfe44_get_ub_size(struct vfe_device *vfe_dev)
75 {
76         return MSM_ISP44_TOTAL_IMAGE_UB;
77 }
78
79 static void msm_vfe44_config_irq(struct vfe_device *vfe_dev,
80                 uint32_t irq0_mask, uint32_t irq1_mask,
81                 enum msm_isp_irq_operation oper)
82 {
83         switch (oper) {
84         case MSM_ISP_IRQ_ENABLE:
85                 vfe_dev->irq0_mask |= irq0_mask;
86                 vfe_dev->irq1_mask |= irq1_mask;
87                 msm_camera_io_w(irq0_mask, vfe_dev->vfe_base + 0x30);
88                 msm_camera_io_w(irq1_mask, vfe_dev->vfe_base + 0x34);
89                 msm_camera_io_w(0x1, vfe_dev->vfe_base + 0x24);
90                 break;
91         case MSM_ISP_IRQ_DISABLE:
92                 vfe_dev->irq0_mask &= ~irq0_mask;
93                 vfe_dev->irq1_mask &= ~irq1_mask;
94                 break;
95         case MSM_ISP_IRQ_SET:
96                 vfe_dev->irq0_mask = irq0_mask;
97                 vfe_dev->irq1_mask = irq1_mask;
98                 msm_camera_io_w(irq0_mask, vfe_dev->vfe_base + 0x30);
99                 msm_camera_io_w(irq1_mask, vfe_dev->vfe_base + 0x34);
100                 msm_camera_io_w(0x1, vfe_dev->vfe_base + 0x24);
101                 break;
102         }
103         msm_camera_io_w_mb(irq0_mask, vfe_dev->vfe_base + 0x28);
104         msm_camera_io_w_mb(irq1_mask, vfe_dev->vfe_base + 0x2C);
105 }
106
107 static int32_t msm_vfe44_init_dt_parms(struct vfe_device *vfe_dev,
108                                 struct msm_vfe_hw_init_parms *dt_parms)
109 {
110         void __iomem *vfebase = vfe_dev->vfe_base;
111         struct device_node *of_node;
112         int32_t i = 0 , rc = 0;
113         uint32_t *dt_settings = NULL, *dt_regs = NULL, dt_entries = 0;
114
115         of_node = vfe_dev->pdev->dev.of_node;
116
117         rc = of_property_read_u32(of_node, dt_parms->entries,
118                 &dt_entries);
119         if (rc < 0 || !dt_entries) {
120                 pr_err("%s: NO QOS entries found\n", __func__);
121                 return -EINVAL;
122         } else {
123                 dt_settings = kzalloc(sizeof(uint32_t) * dt_entries,
124                         GFP_KERNEL);
125                 if (!dt_settings) {
126                         pr_err("%s:%d No memory\n", __func__, __LINE__);
127                         return -ENOMEM;
128                 }
129                 dt_regs = kzalloc(sizeof(uint32_t) * dt_entries,
130                         GFP_KERNEL);
131                 if (!dt_regs) {
132                         pr_err("%s:%d No memory\n", __func__, __LINE__);
133                         kfree(dt_settings);
134                         return -ENOMEM;
135                 }
136                 rc = of_property_read_u32_array(of_node, dt_parms->regs,
137                         dt_regs, dt_entries);
138                 if (rc < 0) {
139                         pr_err("%s: NO QOS BUS BDG info\n", __func__);
140                         kfree(dt_settings);
141                         kfree(dt_regs);
142                         return -EINVAL;
143                 } else {
144                         if (dt_parms->settings) {
145                                 rc = of_property_read_u32_array(of_node,
146                                         dt_parms->settings,
147                                         dt_settings, dt_entries);
148                                 if (rc < 0) {
149                                         pr_err("%s: NO QOS settings\n",
150                                                 __func__);
151                                         kfree(dt_settings);
152                                         kfree(dt_regs);
153                                 } else {
154                                         for (i = 0; i < dt_entries; i++) {
155                                                 msm_camera_io_w(dt_settings[i],
156                                                         vfebase + dt_regs[i]);
157                                         }
158                                         kfree(dt_settings);
159                                         kfree(dt_regs);
160                                 }
161                         } else {
162                                 kfree(dt_settings);
163                                 kfree(dt_regs);
164                         }
165                 }
166         }
167         return 0;
168 }
169
170 static void msm_vfe44_init_hardware_reg(struct vfe_device *vfe_dev)
171 {
172         struct msm_vfe_hw_init_parms qos_parms;
173         struct msm_vfe_hw_init_parms vbif_parms;
174         struct msm_vfe_hw_init_parms ds_parms;
175
176         qos_parms.entries = "qos-entries";
177         qos_parms.regs = "qos-regs";
178         qos_parms.settings = "qos-settings";
179         vbif_parms.entries = "vbif-entries";
180         vbif_parms.regs = "vbif-regs";
181         vbif_parms.settings = "vbif-settings";
182         ds_parms.entries = "ds-entries";
183         ds_parms.regs = "ds-regs";
184         ds_parms.settings = "ds-settings";
185
186         msm_vfe44_init_dt_parms(vfe_dev, &qos_parms);
187         msm_vfe44_init_dt_parms(vfe_dev, &ds_parms);
188         msm_vfe44_init_dt_parms(vfe_dev, &vbif_parms);
189
190         /* BUS_CFG */
191         msm_camera_io_w(0x10000001, vfe_dev->vfe_base + 0x50);
192         msm_vfe44_config_irq(vfe_dev, 0x800000E0, 0xFFFFFF7E,
193                         MSM_ISP_IRQ_ENABLE);
194
195 }
196
197 static void msm_vfe44_clear_status_reg(struct vfe_device *vfe_dev)
198 {
199         msm_vfe44_config_irq(vfe_dev, 0x80000000, 0,
200                         MSM_ISP_IRQ_SET);
201         msm_camera_io_w(0xFFFFFFFF, vfe_dev->vfe_base + 0x30);
202         msm_camera_io_w_mb(0xFFFFFFFF, vfe_dev->vfe_base + 0x34);
203         msm_camera_io_w_mb(0x1, vfe_dev->vfe_base + 0x24);
204 }
205
206 static void msm_vfe44_process_reset_irq(struct vfe_device *vfe_dev,
207         uint32_t irq_status0, uint32_t irq_status1)
208 {
209         if (irq_status0 & (1 << 31)) {
210                 complete(&vfe_dev->reset_complete);
211                 vfe_dev->reset_pending = 0;
212         }
213 }
214
215 static void msm_vfe44_process_halt_irq(struct vfe_device *vfe_dev,
216         uint32_t irq_status0, uint32_t irq_status1)
217 {
218         if (irq_status1 & (1 << 8)) {
219                 complete(&vfe_dev->halt_complete);
220                 msm_camera_io_w(0x0, vfe_dev->vfe_base + 0x2C0);
221         }
222 }
223
224 static void msm_vfe44_process_input_irq(struct vfe_device *vfe_dev,
225         uint32_t irq_status0, uint32_t irq_status1,
226         struct msm_isp_timestamp *ts)
227 {
228         if (!(irq_status0 & 0x1000003))
229                 return;
230
231         if (irq_status0 & (1 << 0)) {
232                 ISP_DBG("%s: SOF IRQ\n", __func__);
233                 msm_isp_increment_frame_id(vfe_dev, VFE_PIX_0, ts);
234         }
235
236         if (irq_status0 & (1 << 24)) {
237                 ISP_DBG("%s: Fetch Engine Read IRQ\n", __func__);
238                 msm_isp_fetch_engine_done_notify(vfe_dev,
239                         &vfe_dev->fetch_engine_info);
240         }
241
242         if (irq_status0 & (1 << 1))
243                 ISP_DBG("%s: EOF IRQ\n", __func__);
244 }
245
246 static void msm_vfe44_process_violation_status(
247         struct vfe_device *vfe_dev)
248 {
249         uint32_t violation_status = vfe_dev->error_info.violation_status;
250         if (!violation_status)
251                 return;
252
253         if (violation_status & (1 << 0))
254                 pr_err("%s: camif violation\n", __func__);
255         if (violation_status & (1 << 1))
256                 pr_err("%s: black violation\n", __func__);
257         if (violation_status & (1 << 2))
258                 pr_err("%s: rolloff violation\n", __func__);
259         if (violation_status & (1 << 3))
260                 pr_err("%s: demux violation\n", __func__);
261         if (violation_status & (1 << 4))
262                 pr_err("%s: demosaic violation\n", __func__);
263         if (violation_status & (1 << 5))
264                 pr_err("%s: wb violation\n", __func__);
265         if (violation_status & (1 << 6))
266                 pr_err("%s: clf violation\n", __func__);
267         if (violation_status & (1 << 7))
268                 pr_err("%s: color correct violation\n", __func__);
269         if (violation_status & (1 << 8))
270                 pr_err("%s: rgb lut violation\n", __func__);
271         if (violation_status & (1 << 9))
272                 pr_err("%s: la violation\n", __func__);
273         if (violation_status & (1 << 10))
274                 pr_err("%s: chroma enhance violation\n", __func__);
275         if (violation_status & (1 << 11))
276                 pr_err("%s: chroma supress mce violation\n", __func__);
277         if (violation_status & (1 << 12))
278                 pr_err("%s: skin enhance violation\n", __func__);
279         if (violation_status & (1 << 13))
280                 pr_err("%s: color tranform enc violation\n", __func__);
281         if (violation_status & (1 << 14))
282                 pr_err("%s: color tranform view violation\n", __func__);
283         if (violation_status & (1 << 15))
284                 pr_err("%s: scale enc y violation\n", __func__);
285         if (violation_status & (1 << 16))
286                 pr_err("%s: scale enc cbcr violation\n", __func__);
287         if (violation_status & (1 << 17))
288                 pr_err("%s: scale view y violation\n", __func__);
289         if (violation_status & (1 << 18))
290                 pr_err("%s: scale view cbcr violation\n", __func__);
291         if (violation_status & (1 << 21))
292                 pr_err("%s: crop enc y violation\n", __func__);
293         if (violation_status & (1 << 22))
294                 pr_err("%s: crop enc cbcr violation\n", __func__);
295         if (violation_status & (1 << 23))
296                 pr_err("%s: crop view y violation\n", __func__);
297         if (violation_status & (1 << 24))
298                 pr_err("%s: crop view cbcr violation\n", __func__);
299         if (violation_status & (1 << 25))
300                 pr_err("%s: realign buf y violation\n", __func__);
301         if (violation_status & (1 << 26))
302                 pr_err("%s: realign buf cb violation\n", __func__);
303         if (violation_status & (1 << 27))
304                 pr_err("%s: realign buf cr violation\n", __func__);
305         if (violation_status & (1 << 28))
306                 pr_err("%s: ltm violation\n", __func__);
307         if (violation_status & (1 << 29))
308                 pr_err("%s: ltm cov violation\n", __func__);
309         if (violation_status & (1 << 30))
310                 pr_err("%s: abf violation\n", __func__);
311         if (violation_status & (1 << 31))
312                 pr_err("%s: bpc violation\n", __func__);
313 }
314
315 static void msm_vfe44_process_error_status(struct vfe_device *vfe_dev)
316 {
317         uint32_t error_status1 = vfe_dev->error_info.error_mask1;
318         if (error_status1 & (1 << 0)) {
319                 pr_err("%s: camif error status: 0x%x\n",
320                         __func__, vfe_dev->error_info.camif_status);
321                 msm_camera_io_dump(vfe_dev->vfe_base + 0x2f4, 0x30, 1);
322         }
323         if (error_status1 & (1 << 1))
324                 pr_err("%s: stats bhist overwrite\n", __func__);
325         if (error_status1 & (1 << 2))
326                 pr_err("%s: stats cs overwrite\n", __func__);
327         if (error_status1 & (1 << 3))
328                 pr_err("%s: stats ihist overwrite\n", __func__);
329         if (error_status1 & (1 << 4))
330                 pr_err("%s: realign buf y overflow\n", __func__);
331         if (error_status1 & (1 << 5))
332                 pr_err("%s: realign buf cb overflow\n", __func__);
333         if (error_status1 & (1 << 6))
334                 pr_err("%s: realign buf cr overflow\n", __func__);
335         if (error_status1 & (1 << 7)) {
336                 msm_vfe44_process_violation_status(vfe_dev);
337         }
338         if (error_status1 & (1 << 9)) {
339                 vfe_dev->stats->imagemaster0_overflow++;
340                 pr_err("%s: image master 0 bus overflow\n", __func__);
341         }
342         if (error_status1 & (1 << 10)) {
343                 vfe_dev->stats->imagemaster1_overflow++;
344                 pr_err("%s: image master 1 bus overflow\n", __func__);
345         }
346         if (error_status1 & (1 << 11)) {
347                 vfe_dev->stats->imagemaster2_overflow++;
348                 pr_err("%s: image master 2 bus overflow\n", __func__);
349         }
350         if (error_status1 & (1 << 12)) {
351                 vfe_dev->stats->imagemaster3_overflow++;
352                 pr_err("%s: image master 3 bus overflow\n", __func__);
353         }
354         if (error_status1 & (1 << 13)) {
355                 vfe_dev->stats->imagemaster4_overflow++;
356                 pr_err("%s: image master 4 bus overflow\n", __func__);
357         }
358         if (error_status1 & (1 << 14)) {
359                 vfe_dev->stats->imagemaster5_overflow++;
360                 pr_err("%s: image master 5 bus overflow\n", __func__);
361         }
362         if (error_status1 & (1 << 15)) {
363                 vfe_dev->stats->imagemaster6_overflow++;
364                 pr_err("%s: image master 6 bus overflow\n", __func__);
365         }
366         if (error_status1 & (1 << 16)) {
367                 vfe_dev->stats->be_overflow++;
368                 pr_err("%s: status be bus overflow\n", __func__);
369         }
370         if (error_status1 & (1 << 17)) {
371                 vfe_dev->stats->bg_overflow++;
372                 pr_err("%s: status bg bus overflow\n", __func__);
373         }
374         if (error_status1 & (1 << 18)) {
375                 vfe_dev->stats->bf_overflow++;
376                 pr_err("%s: status bf bus overflow\n", __func__);
377         }
378         if (error_status1 & (1 << 19)) {
379                 vfe_dev->stats->awb_overflow++;
380                 pr_err("%s: status awb bus overflow\n", __func__);
381         }
382         if (error_status1 & (1 << 20)) {
383                 vfe_dev->stats->rs_overflow++;
384                 pr_err("%s: status rs bus overflow\n", __func__);
385         }
386         if (error_status1 & (1 << 21)) {
387                 vfe_dev->stats->cs_overflow++;
388                 pr_err("%s: status cs bus overflow\n", __func__);
389         }
390         if (error_status1 & (1 << 22)) {
391                 vfe_dev->stats->ihist_overflow++;
392                 pr_err("%s: status ihist bus overflow\n", __func__);
393         }
394         if (error_status1 & (1 << 23)) {
395                 vfe_dev->stats->skinbhist_overflow++;
396                 pr_err("%s: status skin bhist bus overflow\n", __func__);
397         }
398         if (error_status1 & (1 << 24)) {
399                 vfe_dev->stats->bfscale_overflow++;
400                 pr_err("%s: status bf scale bus overflow\n", __func__);
401         }
402 }
403
404 static void msm_vfe44_read_and_clear_irq_status(struct vfe_device *vfe_dev,
405         uint32_t *irq_status0, uint32_t *irq_status1)
406 {
407         *irq_status0 = msm_camera_io_r(vfe_dev->vfe_base + 0x38);
408         *irq_status1 = msm_camera_io_r(vfe_dev->vfe_base + 0x3C);
409
410         msm_camera_io_w(*irq_status0, vfe_dev->vfe_base + 0x30);
411         msm_camera_io_w(*irq_status1, vfe_dev->vfe_base + 0x34);
412         msm_camera_io_w_mb(1, vfe_dev->vfe_base + 0x24);
413         *irq_status0 &= vfe_dev->irq0_mask;
414         *irq_status1 &= vfe_dev->irq1_mask;
415         if (*irq_status0 & 0x10000000) {
416                 pr_err_ratelimited("%s: Protection triggered\n", __func__);
417                 *irq_status0 &= ~(0x10000000);
418         }
419
420         if (*irq_status1 & (1 << 0)) {
421                 vfe_dev->error_info.camif_status =
422                 msm_camera_io_r(vfe_dev->vfe_base + 0x31C);
423                 msm_vfe44_config_irq(vfe_dev, 0, (1 << 0), MSM_ISP_IRQ_DISABLE);
424         }
425
426         if (*irq_status1 & (1 << 7))
427                 vfe_dev->error_info.violation_status =
428                 msm_camera_io_r(vfe_dev->vfe_base + 0x48);
429
430 }
431
432 static void msm_vfe44_read_irq_status(struct vfe_device *vfe_dev,
433          uint32_t *irq_status0, uint32_t *irq_status1)
434 {
435         *irq_status0 = msm_camera_io_r(vfe_dev->vfe_base + 0x38);
436         *irq_status1 = msm_camera_io_r(vfe_dev->vfe_base + 0x3C);
437 }
438
439 static void msm_vfe44_process_reg_update(struct vfe_device *vfe_dev,
440         uint32_t irq_status0, uint32_t irq_status1,
441         struct msm_isp_timestamp *ts)
442 {
443         enum msm_vfe_input_src i;
444         uint32_t shift_irq;
445         uint8_t reg_updated = 0;
446         unsigned long flags;
447
448         if (!(irq_status0 & 0xF0))
449                 return;
450         /* Shift status bits so that PIX SOF is 1st bit */
451         shift_irq = ((irq_status0 & 0xF0) >> 4);
452
453         for (i = VFE_PIX_0; i <= VFE_RAW_2; i++) {
454                 if (shift_irq & BIT(i)) {
455                         reg_updated |= BIT(i);
456                         ISP_DBG("%s REG_UPDATE IRQ %x\n", __func__,
457                                 (uint32_t)BIT(i));
458                         switch (i) {
459                         case VFE_PIX_0:
460                                 msm_isp_notify(vfe_dev, ISP_EVENT_REG_UPDATE,
461                                         VFE_PIX_0, ts);
462                                 msm_isp_process_reg_upd_epoch_irq(vfe_dev, i,
463                                                 MSM_ISP_COMP_IRQ_REG_UPD, ts);
464                                 msm_isp_process_stats_reg_upd_epoch_irq(vfe_dev,
465                                         MSM_ISP_COMP_IRQ_REG_UPD);
466                                 if (vfe_dev->axi_data.src_info[i].stream_count
467                                                                         == 0 &&
468                                         vfe_dev->axi_data.src_info[i].
469                                                 raw_stream_count == 0 &&
470                                         vfe_dev->axi_data.src_info[i].active)
471                                         vfe_dev->hw_info->vfe_ops.core_ops.
472                                                 reg_update(vfe_dev, i);
473                                 break;
474                         case VFE_RAW_0:
475                         case VFE_RAW_1:
476                         case VFE_RAW_2:
477                                 msm_isp_increment_frame_id(vfe_dev, i, ts);
478                                 msm_isp_notify(vfe_dev, ISP_EVENT_SOF, i, ts);
479                                 msm_isp_process_reg_upd_epoch_irq(vfe_dev, i,
480                                                 MSM_ISP_COMP_IRQ_REG_UPD, ts);
481                                 /*
482                                  * Reg Update is pseudo SOF for RDI,
483                                  * so request every frame
484                                  */
485                                 vfe_dev->hw_info->vfe_ops.core_ops.reg_update(
486                                         vfe_dev, i);
487                                 /* reg upd is epoch for rdi */
488                                 msm_isp_process_reg_upd_epoch_irq(vfe_dev, i,
489                                                 MSM_ISP_COMP_IRQ_EPOCH, ts);
490                                 break;
491                         default:
492                                 pr_err("%s: Error case\n", __func__);
493                                 return;
494                         }
495                 }
496         }
497
498         spin_lock_irqsave(&vfe_dev->reg_update_lock, flags);
499         if (reg_updated & BIT(VFE_PIX_0))
500                 vfe_dev->reg_updated = 1;
501
502         vfe_dev->reg_update_requested &= ~reg_updated;
503         spin_unlock_irqrestore(&vfe_dev->reg_update_lock, flags);
504 }
505
506 static void msm_vfe44_process_epoch_irq(struct vfe_device *vfe_dev,
507         uint32_t irq_status0, uint32_t irq_status1,
508         struct msm_isp_timestamp *ts)
509 {
510         if (!(irq_status0 & 0xc))
511                 return;
512
513         if (irq_status0 & BIT(2)) {
514                 msm_isp_notify(vfe_dev, ISP_EVENT_SOF, VFE_PIX_0, ts);
515                 ISP_DBG("%s: EPOCH0 IRQ\n", __func__);
516                 msm_isp_process_reg_upd_epoch_irq(vfe_dev, VFE_PIX_0,
517                                         MSM_ISP_COMP_IRQ_EPOCH, ts);
518                 msm_isp_process_stats_reg_upd_epoch_irq(vfe_dev,
519                                         MSM_ISP_COMP_IRQ_EPOCH);
520                 msm_isp_update_error_frame_count(vfe_dev);
521                 if (vfe_dev->axi_data.src_info[VFE_PIX_0].raw_stream_count > 0
522                         && vfe_dev->axi_data.src_info[VFE_PIX_0].
523                         stream_count == 0) {
524                         ISP_DBG("%s: SOF IRQ\n", __func__);
525                         msm_isp_notify(vfe_dev, ISP_EVENT_SOF, VFE_PIX_0, ts);
526                         msm_isp_process_reg_upd_epoch_irq(vfe_dev, VFE_PIX_0,
527                                                 MSM_ISP_COMP_IRQ_REG_UPD, ts);
528                         vfe_dev->hw_info->vfe_ops.core_ops.reg_update(
529                                    vfe_dev, VFE_PIX_0);
530                 }
531         }
532 }
533
534 static void msm_vfe44_reg_update(struct vfe_device *vfe_dev,
535         enum msm_vfe_input_src frame_src)
536 {
537         uint32_t update_mask = 0;
538         unsigned long flags;
539
540         /* This HW supports upto VFE_RAW_2 */
541         if (frame_src > VFE_RAW_2 && frame_src != VFE_SRC_MAX) {
542                 pr_err("%s Error case\n", __func__);
543                 return;
544         }
545
546         /*
547          * If frame_src == VFE_SRC_MAX request reg_update on
548          * all supported INTF
549          */
550         if (frame_src == VFE_SRC_MAX)
551                 update_mask = 0xF;
552         else
553                 update_mask = BIT((uint32_t)frame_src);
554
555         spin_lock_irqsave(&vfe_dev->reg_update_lock, flags);
556         vfe_dev->axi_data.src_info[VFE_PIX_0].reg_update_frame_id =
557                 vfe_dev->axi_data.src_info[VFE_PIX_0].frame_id;
558         vfe_dev->reg_update_requested |= update_mask;
559         vfe_dev->common_data->dual_vfe_res->reg_update_mask[vfe_dev->pdev->id] =
560                 vfe_dev->reg_update_requested;
561         if ((vfe_dev->is_split && vfe_dev->pdev->id == ISP_VFE1) &&
562                 ((frame_src == VFE_PIX_0) || (frame_src == VFE_SRC_MAX))) {
563                 if (!vfe_dev->common_data->dual_vfe_res->vfe_base[ISP_VFE0]) {
564                         pr_err("%s vfe_base for ISP_VFE0 is NULL\n", __func__);
565                         spin_unlock_irqrestore(&vfe_dev->reg_update_lock,
566                                 flags);
567                         return;
568                 }
569                 msm_camera_io_w_mb(update_mask,
570                         vfe_dev->common_data->dual_vfe_res->vfe_base[ISP_VFE0]
571                         + 0x378);
572                 msm_camera_io_w_mb(update_mask,
573                         vfe_dev->vfe_base + 0x378);
574         } else if (!vfe_dev->is_split ||
575                 ((frame_src == VFE_PIX_0) &&
576                 (vfe_dev->axi_data.src_info[VFE_PIX_0].stream_count == 0) &&
577                 (vfe_dev->axi_data.src_info[VFE_PIX_0].
578                                         raw_stream_count == 0)) ||
579                 (frame_src >= VFE_RAW_0 && frame_src <= VFE_SRC_MAX)) {
580                 msm_camera_io_w_mb(update_mask,
581                         vfe_dev->vfe_base + 0x378);
582         }
583         spin_unlock_irqrestore(&vfe_dev->reg_update_lock, flags);
584 }
585
586 static long msm_vfe44_reset_hardware(struct vfe_device *vfe_dev,
587         uint32_t first_start, uint32_t blocking_call)
588 {
589         long rc = 0;
590         init_completion(&vfe_dev->reset_complete);
591
592         if (blocking_call)
593                 vfe_dev->reset_pending = 1;
594
595         if (first_start) {
596                 msm_camera_io_w_mb(0x1FF, vfe_dev->vfe_base + 0xC);
597         } else {
598                 msm_camera_io_w_mb(0x1EF, vfe_dev->vfe_base + 0xC);
599                 msm_camera_io_w(0x7FFFFFFF, vfe_dev->vfe_base + 0x30);
600                 msm_camera_io_w(0xFEFFFEFF, vfe_dev->vfe_base + 0x34);
601                 msm_camera_io_w(0x1, vfe_dev->vfe_base + 0x24);
602                 vfe_dev->hw_info->vfe_ops.axi_ops.
603                         reload_wm(vfe_dev, vfe_dev->vfe_base, 0x0031FFFF);
604         }
605
606         if (blocking_call) {
607                 rc = wait_for_completion_timeout(
608                         &vfe_dev->reset_complete, msecs_to_jiffies(50));
609                 if (rc <= 0) {
610                         pr_err("%s:%d failed: reset timeout\n", __func__,
611                                 __LINE__);
612                         vfe_dev->reset_pending = 0;
613                 }
614         }
615
616         return rc;
617 }
618
619 static void msm_vfe44_axi_reload_wm(struct vfe_device *vfe_dev,
620         void __iomem *vfe_base, uint32_t reload_mask)
621 {
622         msm_camera_io_w_mb(reload_mask, vfe_base + 0x4C);
623 }
624
625 static void msm_vfe44_axi_enable_wm(void __iomem *vfe_base,
626         uint8_t wm_idx, uint8_t enable)
627 {
628         uint32_t val;
629         val = msm_camera_io_r(vfe_base + VFE44_WM_BASE(wm_idx));
630         if (enable)
631                 val |= 0x1;
632         else
633                 val &= ~0x1;
634         msm_camera_io_w_mb(val,
635                 vfe_base + VFE44_WM_BASE(wm_idx));
636 }
637
638 static void msm_vfe44_axi_update_cgc_override(struct vfe_device *vfe_dev,
639         uint8_t wm_idx, uint8_t cgc_override)
640 {
641         uint32_t val = 0;
642
643         /* Change CGC override */
644         val = msm_camera_io_r(vfe_dev->vfe_base + 0x974);
645         if (cgc_override)
646                 val |= (1 << wm_idx);
647         else
648                 val &= ~(1 << wm_idx);
649         msm_camera_io_w_mb(val, vfe_dev->vfe_base + 0x974);
650 }
651
652 static void msm_vfe44_axi_cfg_comp_mask(struct vfe_device *vfe_dev,
653         struct msm_vfe_axi_stream *stream_info)
654 {
655         struct msm_vfe_axi_shared_data *axi_data = &vfe_dev->axi_data;
656         int vfe_idx = msm_isp_get_vfe_idx_for_stream(vfe_dev, stream_info);
657         uint32_t comp_mask, comp_mask_index;
658
659         comp_mask_index = stream_info->comp_mask_index[vfe_idx];
660
661         comp_mask = msm_camera_io_r(vfe_dev->vfe_base + 0x40);
662         comp_mask &= ~(0x7F << (comp_mask_index * 8));
663         comp_mask |= (axi_data->composite_info[comp_mask_index].
664                 stream_composite_mask << (comp_mask_index * 8));
665         msm_camera_io_w(comp_mask, vfe_dev->vfe_base + 0x40);
666
667         msm_vfe44_config_irq(vfe_dev, 1 << (comp_mask_index + 25), 0,
668                         MSM_ISP_IRQ_ENABLE);
669 }
670
671 static void msm_vfe44_axi_clear_comp_mask(struct vfe_device *vfe_dev,
672         struct msm_vfe_axi_stream *stream_info)
673 {
674         int vfe_idx = msm_isp_get_vfe_idx_for_stream(vfe_dev, stream_info);
675         uint32_t comp_mask, comp_mask_index;
676
677         comp_mask_index = stream_info->comp_mask_index[vfe_idx];
678
679         comp_mask = msm_camera_io_r(vfe_dev->vfe_base + 0x40);
680         comp_mask &= ~(0x7F << (comp_mask_index * 8));
681         msm_camera_io_w(comp_mask, vfe_dev->vfe_base + 0x40);
682
683         msm_vfe44_config_irq(vfe_dev, (1 << (comp_mask_index + 25)), 0,
684                         MSM_ISP_IRQ_DISABLE);
685 }
686
687 static void msm_vfe44_axi_cfg_wm_irq_mask(struct vfe_device *vfe_dev,
688         struct msm_vfe_axi_stream *stream_info)
689 {
690         int vfe_idx = msm_isp_get_vfe_idx_for_stream(vfe_dev, stream_info);
691
692         msm_vfe44_config_irq(vfe_dev, 1 << (stream_info->wm[vfe_idx][0] + 8), 0,
693                         MSM_ISP_IRQ_ENABLE);
694 }
695
696 static void msm_vfe44_axi_clear_wm_irq_mask(struct vfe_device *vfe_dev,
697         struct msm_vfe_axi_stream *stream_info)
698 {
699         int vfe_idx = msm_isp_get_vfe_idx_for_stream(vfe_dev, stream_info);
700
701         msm_vfe44_config_irq(vfe_dev, (1 << (stream_info->wm[vfe_idx][0] + 8)),
702                         0, MSM_ISP_IRQ_DISABLE);
703 }
704
705 static void msm_vfe44_cfg_framedrop(struct vfe_device *vfe_dev,
706         struct msm_vfe_axi_stream *stream_info, uint32_t framedrop_pattern,
707         uint32_t framedrop_period)
708 {
709         void __iomem *vfe_base = vfe_dev->vfe_base;
710         uint32_t i, temp;
711         int vfe_idx = msm_isp_get_vfe_idx_for_stream(vfe_dev, stream_info);
712
713         for (i = 0; i < stream_info->num_planes; i++) {
714                 msm_camera_io_w(framedrop_pattern, vfe_base +
715                         VFE44_WM_BASE(stream_info->wm[vfe_idx][i]) + 0x1C);
716                 temp = msm_camera_io_r(vfe_base +
717                         VFE44_WM_BASE(stream_info->wm[vfe_idx][i]) + 0xC);
718                 temp &= 0xFFFFFF83;
719                 msm_camera_io_w(temp | (framedrop_period - 1) << 2,
720                         vfe_base +
721                         VFE44_WM_BASE(stream_info->wm[vfe_idx][i]) + 0xC);
722         }
723 }
724
725 static void msm_vfe44_clear_framedrop(struct vfe_device *vfe_dev,
726         struct msm_vfe_axi_stream *stream_info)
727 {
728         uint32_t i;
729         int vfe_idx = msm_isp_get_vfe_idx_for_stream(vfe_dev, stream_info);
730
731         for (i = 0; i < stream_info->num_planes; i++)
732                 msm_camera_io_w(0, vfe_dev->vfe_base +
733                         VFE44_WM_BASE(stream_info->wm[vfe_idx][i]) + 0x1C);
734 }
735
736 static int32_t msm_vfe44_convert_bpp_to_reg(int32_t bpp, uint32_t *bpp_reg)
737 {
738         int rc = 0;
739         switch (bpp) {
740         case 8:
741                 *bpp_reg = 0;
742                 break;
743         case 10:
744                 *bpp_reg = 1 << 0;
745                 break;
746         case 12:
747                 *bpp_reg = 1 << 1;
748                 break;
749         default:
750                 pr_err("%s:%d invalid bpp %d", __func__, __LINE__, bpp);
751                 return -EINVAL;
752         }
753
754         return rc;
755 }
756
757 static int32_t msm_vfe44_convert_io_fmt_to_reg(
758         enum msm_isp_pack_fmt pack_format, uint32_t *pack_reg)
759 {
760         int rc = 0;
761
762         switch (pack_format) {
763         case QCOM:
764                 *pack_reg = 0x0;
765                 break;
766         case MIPI:
767                 *pack_reg = 0x1;
768                 break;
769         case DPCM6:
770                 *pack_reg = 0x2;
771                 break;
772         case DPCM8:
773                 *pack_reg = 0x3;
774                 break;
775         case PLAIN8:
776                 *pack_reg = 0x4;
777                 break;
778         case PLAIN16:
779                 *pack_reg = 0x5;
780                 break;
781         default:
782                 pr_err("%s: invalid pack fmt %d!\n", __func__, pack_format);
783                 return -EINVAL;
784         }
785
786         return rc;
787 }
788
789 static int32_t msm_vfe44_cfg_io_format(struct vfe_device *vfe_dev,
790         enum msm_vfe_axi_stream_src stream_src, uint32_t io_format)
791 {
792         int rc = 0;
793         int bpp = 0, read_bpp = 0;
794         enum msm_isp_pack_fmt pack_fmt = 0, read_pack_fmt = 0;
795         uint32_t bpp_reg = 0, pack_reg = 0;
796         uint32_t read_bpp_reg = 0, read_pack_reg = 0;
797         uint32_t io_format_reg = 0; /*io format register bit*/
798
799         io_format_reg = msm_camera_io_r(vfe_dev->vfe_base + 0x54);
800
801         /*input config*/
802         if ((stream_src < RDI_INTF_0) &&
803                 (vfe_dev->axi_data.src_info[VFE_PIX_0].input_mux ==
804                 EXTERNAL_READ)) {
805                 read_bpp = msm_isp_get_bit_per_pixel(
806                         vfe_dev->axi_data.src_info[VFE_PIX_0].input_format);
807                 rc = msm_vfe44_convert_bpp_to_reg(read_bpp, &read_bpp_reg);
808                 if (rc < 0) {
809                         pr_err("%s: convert_bpp_to_reg err! in_bpp %d rc %d\n",
810                                 __func__, read_bpp, rc);
811                         return rc;
812                 }
813
814                 read_pack_fmt = msm_isp_get_pack_format(
815                         vfe_dev->axi_data.src_info[VFE_PIX_0].input_format);
816                 rc = msm_vfe44_convert_io_fmt_to_reg(
817                         read_pack_fmt, &read_pack_reg);
818                 if (rc < 0) {
819                         pr_err("%s: convert_io_fmt_to_reg err! rc = %d\n",
820                                 __func__, rc);
821                         return rc;
822                 }
823                 /*use input format(v4l2_pix_fmt) to get pack format*/
824                 io_format_reg &= 0xFFC8FFFF;
825                 io_format_reg |= (read_bpp_reg << 20 | read_pack_reg << 16);
826         }
827
828         bpp = msm_isp_get_bit_per_pixel(io_format);
829         rc = msm_vfe44_convert_bpp_to_reg(bpp, &bpp_reg);
830         if (rc < 0) {
831                 pr_err("%s: convert_bpp_to_reg err! bpp %d rc = %d\n",
832                         __func__, bpp, rc);
833                 return rc;
834         }
835
836         switch (stream_src) {
837         case PIX_ENCODER:
838         case PIX_VIEWFINDER:
839         case CAMIF_RAW:
840                 io_format_reg &= 0xFFFFCFFF;
841                 io_format_reg |= bpp_reg << 12;
842                 break;
843         case IDEAL_RAW:
844                 /*use output format(v4l2_pix_fmt) to get pack format*/
845                 pack_fmt = msm_isp_get_pack_format(io_format);
846                 rc = msm_vfe44_convert_io_fmt_to_reg(pack_fmt, &pack_reg);
847                 if (rc < 0) {
848                         pr_err("%s: convert_io_fmt_to_reg err! rc = %d\n",
849                                 __func__, rc);
850                         return rc;
851                 }
852                 io_format_reg &= 0xFFFFFFC8;
853                 io_format_reg |= bpp_reg << 4 | pack_reg;
854                 break;
855         case RDI_INTF_0:
856         case RDI_INTF_1:
857         case RDI_INTF_2:
858         default:
859                 pr_err("%s: Invalid stream source\n", __func__);
860                 return -EINVAL;
861         }
862
863         msm_camera_io_w(io_format_reg, vfe_dev->vfe_base + 0x54);
864         return 0;
865 }
866
867 static int msm_vfe44_fetch_engine_start(struct vfe_device *vfe_dev,
868         void *arg)
869 {
870         int rc = 0;
871         uint32_t bufq_handle;
872         struct msm_isp_buffer *buf = NULL;
873         struct msm_vfe_fetch_eng_start *fe_cfg = arg;
874         struct msm_isp_buffer_mapped_info mapped_info;
875
876         if (vfe_dev->fetch_engine_info.is_busy == 1) {
877                 pr_err("%s: fetch engine busy\n", __func__);
878                 return -EINVAL;
879         }
880
881         memset(&mapped_info, 0, sizeof(struct msm_isp_buffer_mapped_info));
882         /* There is other option of passing buffer address from user,
883                 in such case, driver needs to map the buffer and use it*/
884         vfe_dev->fetch_engine_info.session_id = fe_cfg->session_id;
885         vfe_dev->fetch_engine_info.stream_id = fe_cfg->stream_id;
886         vfe_dev->fetch_engine_info.offline_mode = fe_cfg->offline_mode;
887         vfe_dev->fetch_engine_info.fd = fe_cfg->fd;
888
889         if (!fe_cfg->offline_mode) {
890                 bufq_handle = vfe_dev->buf_mgr->ops->get_bufq_handle(
891                         vfe_dev->buf_mgr, fe_cfg->session_id,
892                         fe_cfg->stream_id);
893                 vfe_dev->fetch_engine_info.bufq_handle = bufq_handle;
894
895                 mutex_lock(&vfe_dev->buf_mgr->lock);
896                 rc = vfe_dev->buf_mgr->ops->get_buf_by_index(
897                         vfe_dev->buf_mgr, bufq_handle, fe_cfg->buf_idx, &buf);
898                 if (rc < 0) {
899                         pr_err("%s: No fetch buffer\n", __func__);
900                         mutex_unlock(&vfe_dev->buf_mgr->lock);
901                         return -EINVAL;
902                 }
903                 mapped_info = buf->mapped_info[0];
904                 buf->state = MSM_ISP_BUFFER_STATE_DISPATCHED;
905                 mutex_unlock(&vfe_dev->buf_mgr->lock);
906         } else {
907                 rc = vfe_dev->buf_mgr->ops->map_buf(vfe_dev->buf_mgr,
908                         &mapped_info, fe_cfg->fd);
909                 if (rc < 0) {
910                         pr_err("%s: can not map buffer\n", __func__);
911                         return -EINVAL;
912                 }
913         }
914         vfe_dev->fetch_engine_info.buf_idx = fe_cfg->buf_idx;
915         vfe_dev->fetch_engine_info.is_busy = 1;
916
917         msm_camera_io_w(mapped_info.paddr, vfe_dev->vfe_base + 0x228);
918
919         msm_camera_io_w_mb(0x10000, vfe_dev->vfe_base + 0x4C);
920         msm_camera_io_w_mb(0x20000, vfe_dev->vfe_base + 0x4C);
921
922         ISP_DBG("%s: Fetch Engine ready\n", __func__);
923         return 0;
924 }
925
926 static void msm_vfe44_cfg_fetch_engine(struct vfe_device *vfe_dev,
927         struct msm_vfe_pix_cfg *pix_cfg)
928 {
929         uint32_t x_size_word;
930         struct msm_vfe_fetch_engine_cfg *fe_cfg = NULL;
931         uint32_t temp = 0;
932
933         if (pix_cfg->input_mux == EXTERNAL_READ) {
934                 fe_cfg = &pix_cfg->fetch_engine_cfg;
935                 pr_debug("%s: fetch_dbg wd x ht buf = %d x %d, fe = %d x %d\n",
936                         __func__, fe_cfg->buf_width, fe_cfg->buf_height,
937                         fe_cfg->fetch_width, fe_cfg->fetch_height);
938
939                 vfe_dev->hw_info->vfe_ops.axi_ops.update_cgc_override(vfe_dev,
940                         VFE44_BUS_RD_CGC_OVERRIDE_BIT, 1);
941
942                 temp = msm_camera_io_r(vfe_dev->vfe_base + 0x50);
943                 temp &= 0xFFFFFFFD;
944                 temp |= (1 << 1);
945                 msm_camera_io_w(temp, vfe_dev->vfe_base + 0x50);
946
947                 msm_vfe44_config_irq(vfe_dev, (1 << 24), 0,
948                                         MSM_ISP_IRQ_SET);
949                 msm_camera_io_w((fe_cfg->fetch_height - 1) & 0xFFF,
950                         vfe_dev->vfe_base + 0x238);
951
952                 x_size_word = msm_isp_cal_word_per_line(
953                         vfe_dev->axi_data.src_info[VFE_PIX_0].input_format,
954                         fe_cfg->fetch_width);
955                 msm_camera_io_w((x_size_word - 1) << 16,
956                         vfe_dev->vfe_base + 0x23C);
957
958                 msm_camera_io_w(x_size_word << 16 |
959                         (fe_cfg->buf_height - 1) << 4 | VFE44_FETCH_BURST_LEN,
960                         vfe_dev->vfe_base + 0x240);
961
962                 msm_camera_io_w(0 << 28 | 2 << 25 |
963                 ((fe_cfg->buf_width - 1) & 0x1FFF) << 12 |
964                 ((fe_cfg->buf_height - 1) & 0xFFF), vfe_dev->vfe_base + 0x244);
965
966                 /* need to use formulae to calculate MAIN_UNPACK_PATTERN*/
967                 msm_camera_io_w(0xF6543210, vfe_dev->vfe_base + 0x248);
968                 msm_camera_io_w(0xF, vfe_dev->vfe_base + 0x264);
969
970                 temp = msm_camera_io_r(vfe_dev->vfe_base + 0x1C);
971                 temp |= 2 << 16 | pix_cfg->pixel_pattern;
972                 msm_camera_io_w(temp, vfe_dev->vfe_base + 0x1C);
973
974         } else {
975                 pr_err("%s: Invalid mux configuration - mux: %d", __func__,
976                         pix_cfg->input_mux);
977                 return;
978         }
979
980         return;
981 }
982
983 static void msm_vfe44_cfg_camif(struct vfe_device *vfe_dev,
984         struct msm_vfe_pix_cfg *pix_cfg)
985 {
986         uint16_t first_pixel, last_pixel, first_line, last_line;
987         struct msm_vfe_camif_cfg *camif_cfg = &pix_cfg->camif_cfg;
988         uint32_t val, subsample_period, subsample_pattern;
989         struct msm_vfe_camif_subsample_cfg *subsample_cfg =
990                 &pix_cfg->camif_cfg.subsample_cfg;
991         uint16_t bus_sub_en = 0;
992
993         vfe_dev->dual_vfe_enable = camif_cfg->is_split;
994
995         msm_camera_io_w(pix_cfg->input_mux << 16 | pix_cfg->pixel_pattern,
996                 vfe_dev->vfe_base + 0x1C);
997
998         if (subsample_cfg->pixel_skip || subsample_cfg->line_skip) {
999                 bus_sub_en = 1;
1000                 val = msm_camera_io_r(vfe_dev->vfe_base + 0x2F8);
1001                 val &= 0xFFFFFFDF;
1002                 val = val | bus_sub_en << 5;
1003                 msm_camera_io_w(val, vfe_dev->vfe_base + 0x2F8);
1004                 subsample_cfg->pixel_skip &= 0x0000FFFF;
1005                 subsample_cfg->line_skip  &= 0x0000FFFF;
1006                 msm_camera_io_w((subsample_cfg->line_skip << 16) |
1007                         subsample_cfg->pixel_skip,
1008                         vfe_dev->vfe_base + 0x30C);
1009         }
1010
1011         first_pixel = camif_cfg->first_pixel;
1012         last_pixel = camif_cfg->last_pixel;
1013         first_line = camif_cfg->first_line;
1014         last_line = camif_cfg->last_line;
1015         subsample_period = camif_cfg->subsample_cfg.irq_subsample_period;
1016         subsample_pattern = camif_cfg->subsample_cfg.irq_subsample_pattern;
1017
1018         msm_camera_io_w(camif_cfg->lines_per_frame << 16 |
1019                 camif_cfg->pixels_per_line, vfe_dev->vfe_base + 0x300);
1020
1021         msm_camera_io_w(first_pixel << 16 | last_pixel,
1022         vfe_dev->vfe_base + 0x304);
1023
1024         msm_camera_io_w(first_line << 16 | last_line,
1025         vfe_dev->vfe_base + 0x308);
1026         if (subsample_period && subsample_pattern) {
1027                 val = msm_camera_io_r(vfe_dev->vfe_base + 0x2F8);
1028                 val &= 0xFFE0FFFF;
1029                 val = (subsample_period - 1) << 16;
1030                 msm_camera_io_w(val, vfe_dev->vfe_base + 0x2F8);
1031                 ISP_DBG("%s:camif PERIOD %x PATTERN %x\n",
1032                         __func__,  subsample_period, subsample_pattern);
1033
1034                 val = subsample_pattern;
1035                 msm_camera_io_w(val, vfe_dev->vfe_base + 0x314);
1036         } else {
1037                 msm_camera_io_w(0xFFFFFFFF, vfe_dev->vfe_base + 0x314);
1038         }
1039         val = msm_camera_io_r(vfe_dev->vfe_base + 0x2E8);
1040         val |= camif_cfg->camif_input;
1041         msm_camera_io_w(val, vfe_dev->vfe_base + 0x2E8);
1042
1043 }
1044
1045 static void msm_vfe44_cfg_input_mux(struct vfe_device *vfe_dev,
1046         struct msm_vfe_pix_cfg *pix_cfg)
1047 {
1048         switch (pix_cfg->input_mux) {
1049         case CAMIF:
1050                 msm_vfe44_cfg_camif(vfe_dev, pix_cfg);
1051                 break;
1052         case EXTERNAL_READ:
1053                 msm_vfe44_cfg_fetch_engine(vfe_dev, pix_cfg);
1054                 break;
1055         default:
1056                 pr_err("%s: Unsupported input mux %d\n",
1057                         __func__, pix_cfg->input_mux);
1058         }
1059         return;
1060 }
1061
1062 static void msm_vfe44_update_camif_state(struct vfe_device *vfe_dev,
1063         enum msm_isp_camif_update_state update_state)
1064 {
1065         uint32_t val;
1066         bool bus_en, vfe_en;
1067
1068         if (update_state == NO_UPDATE)
1069                 return;
1070
1071         if (update_state == ENABLE_CAMIF) {
1072                 msm_camera_io_w(0x0, vfe_dev->vfe_base + 0x30);
1073                 msm_camera_io_w_mb(0x81, vfe_dev->vfe_base + 0x34);
1074                 msm_camera_io_w_mb(0x1, vfe_dev->vfe_base + 0x24);
1075
1076                 msm_vfe44_config_irq(vfe_dev, 0xF7, 0x81,
1077                                 MSM_ISP_IRQ_ENABLE);
1078                 msm_camera_io_w_mb(0x140000, vfe_dev->vfe_base + 0x318);
1079
1080                 bus_en =
1081                         ((vfe_dev->axi_data.
1082                         src_info[VFE_PIX_0].raw_stream_count > 0) ? 1 : 0);
1083                 vfe_en =
1084                         ((vfe_dev->axi_data.
1085                         src_info[VFE_PIX_0].stream_count > 0) ? 1 : 0);
1086                 val = msm_camera_io_r(vfe_dev->vfe_base + 0x2F8);
1087                 val &= 0xFFFFFF3F;
1088                 val = val | bus_en << 7 | vfe_en << 6;
1089                 msm_camera_io_w(val, vfe_dev->vfe_base + 0x2F8);
1090                 msm_camera_io_w_mb(0x4, vfe_dev->vfe_base + 0x2F4);
1091                 msm_camera_io_w_mb(0x1, vfe_dev->vfe_base + 0x2F4);
1092
1093                 vfe_dev->axi_data.src_info[VFE_PIX_0].active = 1;
1094         } else if (update_state == DISABLE_CAMIF ||
1095                 DISABLE_CAMIF_IMMEDIATELY == update_state) {
1096                 uint32_t poll_val;
1097
1098                 if (vfe_dev->axi_data.src_info[VFE_PIX_0].input_mux == TESTGEN)
1099                         update_state = DISABLE_CAMIF;
1100                 msm_vfe44_config_irq(vfe_dev, 0,
1101                         0x81, MSM_ISP_IRQ_DISABLE);
1102                 val = msm_camera_io_r(vfe_dev->vfe_base + 0xC18);
1103                 /* disable danger signal */
1104                 msm_camera_io_w_mb(val & ~(1 << 8), vfe_dev->vfe_base + 0xC18);
1105                 msm_camera_io_w_mb((update_state == DISABLE_CAMIF ? 0x0 : 0x6),
1106                                 vfe_dev->vfe_base + 0x2F4);
1107                 if (readl_poll_timeout_atomic(vfe_dev->vfe_base + 0x31C,
1108                                 poll_val, poll_val & 0x80000000, 1000, 2000000))
1109                         pr_err("%s: camif disable failed %x\n",
1110                                 __func__, poll_val);
1111                 vfe_dev->axi_data.src_info[VFE_PIX_0].active = 0;
1112                 msm_camera_io_w(0, vfe_dev->vfe_base + 0x30);
1113                 msm_camera_io_w((1 << 0), vfe_dev->vfe_base + 0x34);
1114                 msm_camera_io_w_mb(1, vfe_dev->vfe_base + 0x24);
1115                 msm_vfe44_config_irq(vfe_dev, vfe_dev->irq0_mask,
1116                         vfe_dev->irq1_mask, MSM_ISP_IRQ_SET);
1117         }
1118 }
1119
1120 static void msm_vfe44_cfg_rdi_reg(
1121         struct vfe_device *vfe_dev, struct msm_vfe_rdi_cfg *rdi_cfg,
1122         enum msm_vfe_input_src input_src)
1123 {
1124         uint8_t rdi = input_src - VFE_RAW_0;
1125         uint32_t rdi_reg_cfg;
1126         rdi_reg_cfg = msm_camera_io_r(
1127                 vfe_dev->vfe_base + VFE44_RDI_BASE(0));
1128         rdi_reg_cfg &= ~(BIT(16 + rdi));
1129         rdi_reg_cfg |= rdi_cfg->frame_based << (16 + rdi);
1130         msm_camera_io_w(rdi_reg_cfg,
1131                 vfe_dev->vfe_base + VFE44_RDI_BASE(0));
1132
1133         rdi_reg_cfg = msm_camera_io_r(
1134                 vfe_dev->vfe_base + VFE44_RDI_BASE(rdi));
1135         rdi_reg_cfg &= 0x70003;
1136         rdi_reg_cfg |= (rdi * 3) << 28 | rdi_cfg->cid << 4 | 0x4;
1137         msm_camera_io_w(
1138                 rdi_reg_cfg, vfe_dev->vfe_base + VFE44_RDI_BASE(rdi));
1139 }
1140
1141 static void msm_vfe44_axi_cfg_wm_reg(
1142         struct vfe_device *vfe_dev,
1143         struct msm_vfe_axi_stream *stream_info,
1144         uint8_t plane_idx)
1145 {
1146         uint32_t val;
1147         int vfe_idx = msm_isp_get_vfe_idx_for_stream(vfe_dev, stream_info);
1148         uint32_t wm_base;
1149
1150         wm_base = VFE44_WM_BASE(stream_info->wm[vfe_idx][plane_idx]);
1151
1152         if (!stream_info->frame_based) {
1153                 msm_camera_io_w(0x0, vfe_dev->vfe_base + wm_base);
1154                 /*WR_IMAGE_SIZE*/
1155                 val =
1156                         ((msm_isp_cal_word_per_line(
1157                                 stream_info->output_format,
1158                                 stream_info->plane_cfg[vfe_idx][plane_idx].
1159                                 output_width)+1)/2 - 1) << 16 |
1160                                 (stream_info->plane_cfg[vfe_idx][plane_idx].
1161                                 output_height - 1);
1162                 msm_camera_io_w(val, vfe_dev->vfe_base + wm_base + 0x14);
1163
1164                 /*WR_BUFFER_CFG*/
1165                 val = (stream_info->plane_cfg[vfe_idx][plane_idx].
1166                                 output_height - 1);
1167                 val = (((val & 0xfff) << 2) | ((val >> 12) & 0x3));
1168                 val = val << 2 |
1169                         msm_isp_cal_word_per_line(stream_info->output_format,
1170                         stream_info->plane_cfg[vfe_idx][
1171                                 plane_idx].output_stride) << 16 |
1172                         VFE44_BURST_LEN;
1173                 msm_camera_io_w(val, vfe_dev->vfe_base + wm_base + 0x18);
1174         } else {
1175                 msm_camera_io_w(0x2, vfe_dev->vfe_base + wm_base);
1176                 val = (stream_info->plane_cfg[vfe_idx][plane_idx].
1177                                                         output_height - 1);
1178                 val = (((val & 0xfff) << 2) | ((val >> 12) & 0x3));
1179                 val = val << 2 |
1180                         msm_isp_cal_word_per_line(stream_info->output_format,
1181                         stream_info->plane_cfg[vfe_idx][
1182                                 plane_idx].output_width) << 16 |
1183                         VFE44_BURST_LEN;
1184                 msm_camera_io_w(val, vfe_dev->vfe_base + wm_base + 0x18);
1185         }
1186
1187         /*WR_IRQ_SUBSAMPLE_PATTERN*/
1188         msm_camera_io_w(0xFFFFFFFF,
1189                 vfe_dev->vfe_base + wm_base + 0x20);
1190         /* TD: Add IRQ subsample pattern */
1191 }
1192
1193 static void msm_vfe44_axi_clear_wm_reg(
1194         struct vfe_device *vfe_dev,
1195         struct msm_vfe_axi_stream *stream_info, uint8_t plane_idx)
1196 {
1197         uint32_t val = 0;
1198         int vfe_idx = msm_isp_get_vfe_idx_for_stream(vfe_dev, stream_info);
1199         uint32_t wm_base;
1200
1201         wm_base = VFE44_WM_BASE(stream_info->wm[vfe_idx][plane_idx]);
1202         /*WR_ADDR_CFG*/
1203         msm_camera_io_w(val, vfe_dev->vfe_base + wm_base + 0xC);
1204         /*WR_IMAGE_SIZE*/
1205         msm_camera_io_w(val, vfe_dev->vfe_base + wm_base + 0x14);
1206         /*WR_BUFFER_CFG*/
1207         msm_camera_io_w(val, vfe_dev->vfe_base + wm_base + 0x18);
1208         /*WR_IRQ_SUBSAMPLE_PATTERN*/
1209         msm_camera_io_w(val, vfe_dev->vfe_base + wm_base + 0x20);
1210 }
1211
1212 static void msm_vfe44_axi_cfg_wm_xbar_reg(
1213         struct vfe_device *vfe_dev,
1214         struct msm_vfe_axi_stream *stream_info,
1215         uint8_t plane_idx)
1216 {
1217         int vfe_idx = msm_isp_get_vfe_idx_for_stream(vfe_dev, stream_info);
1218         struct msm_vfe_axi_plane_cfg *plane_cfg;
1219         uint8_t wm;
1220         uint32_t xbar_cfg = 0;
1221         uint32_t xbar_reg_cfg = 0;
1222
1223         plane_cfg = &stream_info->plane_cfg[vfe_idx][plane_idx];
1224         wm = stream_info->wm[vfe_idx][plane_idx];
1225
1226         switch (stream_info->stream_src) {
1227         case PIX_ENCODER:
1228         case PIX_VIEWFINDER: {
1229                 if (plane_cfg->output_plane_format != CRCB_PLANE &&
1230                         plane_cfg->output_plane_format != CBCR_PLANE) {
1231                         /*SINGLE_STREAM_SEL*/
1232                         xbar_cfg |= plane_cfg->output_plane_format << 8;
1233                 } else {
1234                         switch (stream_info->output_format) {
1235                         case V4L2_PIX_FMT_NV12:
1236                         case V4L2_PIX_FMT_NV14:
1237                         case V4L2_PIX_FMT_NV16:
1238                         case V4L2_PIX_FMT_NV24:
1239                                 xbar_cfg |= 0x3 << 4; /*PAIR_STREAM_SWAP_CTRL*/
1240                                 break;
1241                         }
1242                         xbar_cfg |= 0x1 << 1; /*PAIR_STREAM_EN*/
1243                 }
1244                 if (stream_info->stream_src == PIX_VIEWFINDER)
1245                         xbar_cfg |= 0x1; /*VIEW_STREAM_EN*/
1246                 break;
1247         }
1248         case CAMIF_RAW:
1249                 xbar_cfg = 0x300;
1250                 break;
1251         case IDEAL_RAW:
1252                 xbar_cfg = 0x400;
1253                 break;
1254         case RDI_INTF_0:
1255                 xbar_cfg = 0x500;
1256                 break;
1257         case RDI_INTF_1:
1258                 xbar_cfg = 0x600;
1259                 break;
1260         case RDI_INTF_2:
1261                 xbar_cfg = 0x700;
1262                 break;
1263         default:
1264                 pr_err("%s: Invalid stream src\n", __func__);
1265                 break;
1266         }
1267         xbar_reg_cfg =
1268                 msm_camera_io_r(vfe_dev->vfe_base + VFE44_XBAR_BASE(wm));
1269         xbar_reg_cfg &= ~(0xFFFF << VFE44_XBAR_SHIFT(wm));
1270         xbar_reg_cfg |= (xbar_cfg << VFE44_XBAR_SHIFT(wm));
1271         msm_camera_io_w(xbar_reg_cfg,
1272                 vfe_dev->vfe_base + VFE44_XBAR_BASE(wm));
1273 }
1274
1275 static void msm_vfe44_axi_clear_wm_xbar_reg(
1276         struct vfe_device *vfe_dev,
1277         struct msm_vfe_axi_stream *stream_info, uint8_t plane_idx)
1278 {
1279         int vfe_idx = msm_isp_get_vfe_idx_for_stream(vfe_dev, stream_info);
1280         uint8_t wm;
1281         uint32_t xbar_reg_cfg = 0;
1282
1283         wm = stream_info->wm[vfe_idx][plane_idx];
1284
1285         xbar_reg_cfg =
1286                 msm_camera_io_r(vfe_dev->vfe_base + VFE44_XBAR_BASE(wm));
1287         xbar_reg_cfg &= ~(0xFFFF << VFE44_XBAR_SHIFT(wm));
1288         msm_camera_io_w(xbar_reg_cfg,
1289                 vfe_dev->vfe_base + VFE44_XBAR_BASE(wm));
1290 }
1291
1292 static void msm_vfe44_read_wm_ping_pong_addr(
1293         struct vfe_device *vfe_dev)
1294 {
1295         msm_camera_io_dump(vfe_dev->vfe_base +
1296                 (VFE44_WM_BASE(0) & 0xFFFFFFF0), 0x200, 1);
1297 }
1298
1299 static void msm_vfe44_update_ping_pong_addr(
1300         void __iomem *vfe_base,
1301         uint8_t wm_idx, uint32_t pingpong_bit, dma_addr_t paddr,
1302         int32_t buf_size)
1303 {
1304         uint32_t paddr32 = (paddr & 0xFFFFFFFF);
1305         msm_camera_io_w(paddr32, vfe_base +
1306                 VFE44_PING_PONG_BASE(wm_idx, pingpong_bit));
1307 }
1308
1309 static void msm_vfe44_set_halt_restart_mask(struct vfe_device *vfe_dev)
1310 {
1311         msm_vfe44_config_irq(vfe_dev, BIT(31), BIT(8), MSM_ISP_IRQ_SET);
1312 }
1313
1314
1315 static int msm_vfe44_axi_halt(struct vfe_device *vfe_dev,
1316         uint32_t blocking)
1317 {
1318         int rc = 0;
1319         enum msm_vfe_input_src i;
1320         struct msm_isp_timestamp ts;
1321
1322         /* Keep only halt and restart mask */
1323         msm_vfe44_config_irq(vfe_dev, (1 << 31), (1 << 8),
1324                         MSM_ISP_IRQ_SET);
1325
1326         if (atomic_read(&vfe_dev->error_info.overflow_state)
1327                 == OVERFLOW_DETECTED)
1328                 pr_err_ratelimited("%s: VFE%d halt for recovery, blocking %d\n",
1329                         __func__, vfe_dev->pdev->id, blocking);
1330
1331         if (blocking) {
1332                 init_completion(&vfe_dev->halt_complete);
1333                 /* Halt AXI Bus Bridge */
1334                 msm_camera_io_w_mb(0x1, vfe_dev->vfe_base + 0x2C0);
1335                 rc = wait_for_completion_timeout(
1336                         &vfe_dev->halt_complete, msecs_to_jiffies(500));
1337                 if (rc <= 0)
1338                         pr_err("%s:VFE%d halt timeout rc=%d\n", __func__,
1339                                 vfe_dev->pdev->id, rc);
1340         } else {
1341                 /* Halt AXI Bus Bridge */
1342                 msm_camera_io_w_mb(0x1, vfe_dev->vfe_base + 0x2C0);
1343         }
1344
1345         msm_isp_get_timestamp(&ts, vfe_dev);
1346         for (i = VFE_PIX_0; i <= VFE_RAW_2; i++) {
1347                 /* if any stream is waiting for update, signal complete */
1348                 msm_isp_axi_stream_update(vfe_dev, i, &ts);
1349                 msm_isp_axi_stream_update(vfe_dev, i, &ts);
1350         }
1351
1352         msm_isp_stats_stream_update(vfe_dev);
1353         msm_isp_stats_stream_update(vfe_dev);
1354
1355         return rc;
1356 }
1357
1358 static void msm_vfe44_axi_restart(struct vfe_device *vfe_dev,
1359         uint32_t blocking, uint32_t enable_camif)
1360 {
1361         msm_vfe44_config_irq(vfe_dev, vfe_dev->recovery_irq0_mask,
1362                         vfe_dev->recovery_irq1_mask, MSM_ISP_IRQ_ENABLE);
1363         msm_camera_io_w_mb(0x140000, vfe_dev->vfe_base + 0x318);
1364
1365         /* Start AXI */
1366         msm_camera_io_w(0x0, vfe_dev->vfe_base + 0x2C0);
1367
1368         vfe_dev->hw_info->vfe_ops.core_ops.reg_update(vfe_dev, VFE_SRC_MAX);
1369         memset(&vfe_dev->error_info, 0, sizeof(vfe_dev->error_info));
1370         atomic_set(&vfe_dev->error_info.overflow_state, NO_OVERFLOW);
1371         if (enable_camif)
1372                 vfe_dev->hw_info->vfe_ops.core_ops.
1373                         update_camif_state(vfe_dev, ENABLE_CAMIF);
1374 }
1375
1376 static uint32_t msm_vfe44_get_wm_mask(
1377         uint32_t irq_status0, uint32_t irq_status1)
1378 {
1379         return (irq_status0 >> 8) & 0x7F;
1380 }
1381
1382 static uint32_t msm_vfe44_get_comp_mask(
1383         uint32_t irq_status0, uint32_t irq_status1)
1384 {
1385         return (irq_status0 >> 25) & 0xF;
1386 }
1387
1388 static uint32_t msm_vfe44_get_pingpong_status(
1389         struct vfe_device *vfe_dev)
1390 {
1391         return msm_camera_io_r(vfe_dev->vfe_base + 0x268);
1392 }
1393
1394 static int msm_vfe44_get_stats_idx(enum msm_isp_stats_type stats_type)
1395 {
1396         switch (stats_type) {
1397         case MSM_ISP_STATS_BE:
1398                 return STATS_IDX_BE;
1399         case MSM_ISP_STATS_BG:
1400                 return STATS_IDX_BG;
1401         case MSM_ISP_STATS_BF:
1402                 return STATS_IDX_BF;
1403         case MSM_ISP_STATS_AWB:
1404                 return STATS_IDX_AWB;
1405         case MSM_ISP_STATS_RS:
1406                 return STATS_IDX_RS;
1407         case MSM_ISP_STATS_CS:
1408                 return STATS_IDX_CS;
1409         case MSM_ISP_STATS_IHIST:
1410                 return STATS_IDX_IHIST;
1411         case MSM_ISP_STATS_BHIST:
1412                 return STATS_IDX_BHIST;
1413         case MSM_ISP_STATS_BF_SCALE:
1414                 return STATS_IDX_BF_SCALE;
1415         default:
1416                 pr_err("%s: Invalid stats type\n", __func__);
1417                 return -EINVAL;
1418         }
1419 }
1420
1421 static int msm_vfe44_stats_check_streams(
1422         struct msm_vfe_stats_stream *stream_info)
1423 {
1424         if (stream_info[STATS_IDX_BF].state ==
1425                 STATS_AVAILABLE &&
1426                 stream_info[STATS_IDX_BF_SCALE].state !=
1427                 STATS_AVAILABLE) {
1428                 pr_err("%s: does not support BF_SCALE while BF is disabled\n",
1429                         __func__);
1430                 return -EINVAL;
1431         }
1432         if (stream_info[STATS_IDX_BF].state != STATS_AVAILABLE &&
1433                 stream_info[STATS_IDX_BF_SCALE].state != STATS_AVAILABLE &&
1434                 stream_info[STATS_IDX_BF].composite_flag !=
1435                 stream_info[STATS_IDX_BF_SCALE].composite_flag) {
1436                 pr_err("%s: Different composite flag for BF and BF_SCALE\n",
1437                         __func__);
1438                 return -EINVAL;
1439         }
1440         return 0;
1441 }
1442
1443 static void msm_vfe44_stats_cfg_comp_mask(
1444         struct vfe_device *vfe_dev,
1445         uint32_t stats_mask, uint8_t request_comp_index, uint8_t enable)
1446 {
1447         uint32_t comp_mask_reg, mask_bf_scale;
1448         atomic_t *stats_comp_mask;
1449         struct msm_vfe_stats_shared_data *stats_data = &vfe_dev->stats_data;
1450
1451         if (vfe_dev->hw_info->stats_hw_info->num_stats_comp_mask < 1)
1452                 return;
1453
1454         if (request_comp_index >= MAX_NUM_STATS_COMP_MASK) {
1455                 pr_err("%s: num of comp masks %d exceed max %d\n",
1456                         __func__, request_comp_index,
1457                         MAX_NUM_STATS_COMP_MASK);
1458                 return;
1459         }
1460
1461         if (vfe_dev->hw_info->stats_hw_info->num_stats_comp_mask >
1462                         MAX_NUM_STATS_COMP_MASK) {
1463                 pr_err("%s: num of comp masks %d exceed max %d\n",
1464                         __func__,
1465                         vfe_dev->hw_info->stats_hw_info->num_stats_comp_mask,
1466                         MAX_NUM_STATS_COMP_MASK);
1467                 return;
1468         }
1469
1470         /* BF scale is controlled by BF also so ignore bit 0 of BF scale */
1471         stats_mask = stats_mask & 0x1FF;
1472         mask_bf_scale = stats_mask >> SHIFT_BF_SCALE_BIT;
1473
1474         stats_comp_mask = &stats_data->stats_comp_mask[request_comp_index];
1475         comp_mask_reg = msm_camera_io_r(vfe_dev->vfe_base + 0x44);
1476
1477         if (enable) {
1478                 comp_mask_reg |= mask_bf_scale << (16 + request_comp_index * 8);
1479                 atomic_set(stats_comp_mask, stats_mask |
1480                                 atomic_read(stats_comp_mask));
1481                 msm_vfe44_config_irq(vfe_dev,
1482                         1 << (request_comp_index + 29), 0,
1483                         MSM_ISP_IRQ_ENABLE);
1484         } else {
1485                 if (!(atomic_read(stats_comp_mask) & stats_mask))
1486                         return;
1487                 if (stats_mask & (1 << STATS_IDX_BF_SCALE) &&
1488                         atomic_read(stats_comp_mask) &
1489                                 (1 << STATS_IDX_BF_SCALE))
1490                         atomic_set(stats_comp_mask,
1491                                         ~(1 << STATS_IDX_BF_SCALE) &
1492                                         atomic_read(stats_comp_mask));
1493
1494                 atomic_set(stats_comp_mask,
1495                                 ~stats_mask & atomic_read(stats_comp_mask));
1496                 comp_mask_reg &= ~(mask_bf_scale <<
1497                         (16 + request_comp_index * 8));
1498                 msm_vfe44_config_irq(vfe_dev,
1499                         1 << (request_comp_index + 29), 0,
1500                         MSM_ISP_IRQ_DISABLE);
1501         }
1502         msm_camera_io_w(comp_mask_reg, vfe_dev->vfe_base + 0x44);
1503
1504         ISP_DBG("%s: comp_mask_reg: %x comp mask0 %x mask1: %x\n",
1505                 __func__, comp_mask_reg,
1506                 atomic_read(&stats_data->stats_comp_mask[0]),
1507                 atomic_read(&stats_data->stats_comp_mask[1]));
1508
1509         return;
1510 }
1511
1512 static void msm_vfe44_stats_cfg_wm_irq_mask(
1513         struct vfe_device *vfe_dev,
1514         struct msm_vfe_stats_stream *stream_info)
1515 {
1516         int vfe_idx = msm_isp_get_vfe_idx_for_stats_stream(vfe_dev,
1517                                 stream_info);
1518
1519         msm_vfe44_config_irq(vfe_dev,
1520                 1 << (STATS_IDX(stream_info->stream_handle[vfe_idx]) + 15), 0,
1521                 MSM_ISP_IRQ_ENABLE);
1522 }
1523
1524 static void msm_vfe44_stats_clear_wm_irq_mask(
1525         struct vfe_device *vfe_dev,
1526         struct msm_vfe_stats_stream *stream_info)
1527 {
1528         int vfe_idx = msm_isp_get_vfe_idx_for_stats_stream(vfe_dev,
1529                                 stream_info);
1530
1531         msm_vfe44_config_irq(vfe_dev,
1532                 (1 << (STATS_IDX(stream_info->stream_handle[vfe_idx]) + 15)), 0,
1533                 MSM_ISP_IRQ_DISABLE);
1534 }
1535
1536 static void msm_vfe44_stats_cfg_wm_reg(
1537         struct vfe_device *vfe_dev,
1538         struct msm_vfe_stats_stream *stream_info)
1539 {
1540         int vfe_idx = msm_isp_get_vfe_idx_for_stats_stream(vfe_dev,
1541                                 stream_info);
1542         int stats_idx;
1543         uint32_t stats_base;
1544
1545         stats_idx = STATS_IDX(stream_info->stream_handle[vfe_idx]);
1546         stats_base = VFE44_STATS_BASE(stats_idx);
1547         /* BF_SCALE does not have its own WR_ADDR_CFG,
1548          * IRQ_FRAMEDROP_PATTERN and IRQ_SUBSAMPLE_PATTERN;
1549          * it's using the same from BF */
1550         if (stats_idx == STATS_IDX_BF_SCALE)
1551                 return;
1552         /*WR_ADDR_CFG*/
1553         msm_camera_io_w((stream_info->framedrop_period - 1) << 2,
1554                 vfe_dev->vfe_base + stats_base + 0x8);
1555         /*WR_IRQ_FRAMEDROP_PATTERN*/
1556         msm_camera_io_w(stream_info->framedrop_pattern,
1557                 vfe_dev->vfe_base + stats_base + 0x10);
1558         /*WR_IRQ_SUBSAMPLE_PATTERN*/
1559         msm_camera_io_w(0xFFFFFFFF,
1560                 vfe_dev->vfe_base + stats_base + 0x14);
1561 }
1562
1563 static void msm_vfe44_stats_clear_wm_reg(
1564         struct vfe_device *vfe_dev,
1565         struct msm_vfe_stats_stream *stream_info)
1566 {
1567         int vfe_idx = msm_isp_get_vfe_idx_for_stats_stream(vfe_dev,
1568                                 stream_info);
1569         uint32_t val = 0;
1570         int stats_idx;
1571         uint32_t stats_base;
1572
1573         stats_idx = STATS_IDX(stream_info->stream_handle[vfe_idx]);
1574         stats_base = VFE44_STATS_BASE(stats_idx);
1575         /* BF_SCALE does not have its own WR_ADDR_CFG,
1576          * IRQ_FRAMEDROP_PATTERN and IRQ_SUBSAMPLE_PATTERN;
1577          * it's using the same from BF */
1578         if (stats_idx == STATS_IDX_BF_SCALE)
1579                 return;
1580
1581         /*WR_ADDR_CFG*/
1582         msm_camera_io_w(val, vfe_dev->vfe_base + stats_base + 0x8);
1583         /*WR_IRQ_FRAMEDROP_PATTERN*/
1584         msm_camera_io_w(val, vfe_dev->vfe_base + stats_base + 0x10);
1585         /*WR_IRQ_SUBSAMPLE_PATTERN*/
1586         msm_camera_io_w(val, vfe_dev->vfe_base + stats_base + 0x14);
1587 }
1588
1589 static void msm_vfe44_stats_cfg_ub(struct vfe_device *vfe_dev)
1590 {
1591         int i;
1592         uint32_t ub_offset = VFE44_UB_SIZE;
1593         uint32_t ub_size[VFE44_NUM_STATS_TYPE] = {
1594                 128, /*MSM_ISP_STATS_BF_SCALE*/
1595                 64, /*MSM_ISP_STATS_BE*/
1596                 128, /*MSM_ISP_STATS_BG*/
1597                 128, /*MSM_ISP_STATS_BF*/
1598                 16, /*MSM_ISP_STATS_AWB*/
1599                 8,  /*MSM_ISP_STATS_RS*/
1600                 16, /*MSM_ISP_STATS_CS*/
1601                 16, /*MSM_ISP_STATS_IHIST*/
1602                 16, /*MSM_ISP_STATS_BHIST*/
1603         };
1604
1605         for (i = 0; i < VFE44_NUM_STATS_TYPE; i++) {
1606                 ub_offset -= ub_size[i];
1607                 msm_camera_io_w(VFE44_STATS_BURST_LEN << 30 |
1608                         ub_offset << 16 | (ub_size[i] - 1),
1609                         vfe_dev->vfe_base + VFE44_STATS_BASE(i) +
1610                         ((i == STATS_IDX_BF_SCALE) ? 0x8 : 0xC));
1611         }
1612 }
1613
1614 static bool msm_vfe44_is_module_cfg_lock_needed(
1615         uint32_t reg_offset)
1616 {
1617         if (reg_offset == 0x18)
1618                 return true;
1619         else
1620                 return false;
1621 }
1622
1623 static void msm_vfe44_stats_enable_module(struct vfe_device *vfe_dev,
1624         uint32_t stats_mask, uint8_t enable)
1625 {
1626         int i;
1627         uint32_t module_cfg, module_cfg_mask = 0;
1628         uint32_t stats_cfg, stats_cfg_mask = 0;
1629         unsigned long flags;
1630
1631         for (i = 0; i < VFE44_NUM_STATS_TYPE; i++) {
1632                 if ((stats_mask >> i) & 0x1) {
1633                         switch (i) {
1634                         case STATS_IDX_BE:
1635                         case STATS_IDX_BG:
1636                         case STATS_IDX_BF:
1637                         case STATS_IDX_AWB:
1638                         case STATS_IDX_RS:
1639                         case STATS_IDX_CS:
1640                                 module_cfg_mask |= 1 << (4 + i);
1641                                 break;
1642                         case STATS_IDX_IHIST:
1643                                 module_cfg_mask |= 1 << 15;
1644                                 break;
1645                         case STATS_IDX_BHIST:
1646                                 module_cfg_mask |= 1 << 18;
1647                                 break;
1648                         case STATS_IDX_BF_SCALE:
1649                                 stats_cfg_mask |= 1 << 2;
1650                                 break;
1651                         default:
1652                                 pr_err("%s: Invalid stats mask\n", __func__);
1653                                 return;
1654                         }
1655                 }
1656         }
1657
1658         /*
1659          * For vfe44 stats and other modules share module_cfg register.
1660          * Hence need to Grab lock.
1661          */
1662         spin_lock_irqsave(&vfe_dev->shared_data_lock, flags);
1663         module_cfg = msm_camera_io_r(vfe_dev->vfe_base + 0x18);
1664         if (enable)
1665                 module_cfg |= module_cfg_mask;
1666         else
1667                 module_cfg &= ~module_cfg_mask;
1668         msm_camera_io_w(module_cfg, vfe_dev->vfe_base + 0x18);
1669         spin_unlock_irqrestore(&vfe_dev->shared_data_lock, flags);
1670
1671         stats_cfg = msm_camera_io_r(vfe_dev->vfe_base + 0x888);
1672         if (enable)
1673                 stats_cfg |= stats_cfg_mask;
1674         else
1675                 stats_cfg &= ~stats_cfg_mask;
1676         msm_camera_io_w(stats_cfg, vfe_dev->vfe_base + 0x888);
1677 }
1678
1679 static void msm_vfe44_stats_update_cgc_override(struct vfe_device *vfe_dev,
1680         uint32_t stats_mask, uint8_t cgc_override)
1681 {
1682         int i;
1683         uint32_t val = 0, cgc_mask = 0;
1684
1685         for (i = 0; i < VFE44_NUM_STATS_TYPE; i++) {
1686                 if ((stats_mask >> i) & 0x1) {
1687                         switch (i) {
1688                         case STATS_IDX_BE:
1689                                 cgc_mask |= (1 << 8);
1690                                 break;
1691                         case STATS_IDX_BG:
1692                                 cgc_mask |= (1 << 9);
1693                                 break;
1694                         case STATS_IDX_BF:
1695                                 cgc_mask |= (1 << 10);
1696                                 break;
1697                         case STATS_IDX_AWB:
1698                                 cgc_mask |= (1 << 11);
1699                                 break;
1700                         case STATS_IDX_RS:
1701                                 cgc_mask |= (1 << 12);
1702                                 break;
1703                         case STATS_IDX_CS:
1704                                 cgc_mask |= (1 << 13);
1705                                 break;
1706                         case STATS_IDX_IHIST:
1707                                 cgc_mask |= (1 << 14);
1708                                 break;
1709                         case STATS_IDX_BHIST:
1710                                 cgc_mask |= (1 << 15);
1711                                 break;
1712                         case STATS_IDX_BF_SCALE:
1713                                 cgc_mask |= (1 << 10);
1714                                 break;
1715                         default:
1716                                 pr_err("%s: Invalid stats mask\n", __func__);
1717                                 return;
1718                         }
1719                 }
1720         }
1721
1722         /* CGC override */
1723         val = msm_camera_io_r(vfe_dev->vfe_base + 0x974);
1724         if (cgc_override)
1725                 val |= cgc_mask;
1726         else
1727                 val &= ~cgc_mask;
1728         msm_camera_io_w(val, vfe_dev->vfe_base + 0x974);
1729 }
1730
1731 static void msm_vfe44_stats_update_ping_pong_addr(
1732         struct vfe_device *vfe_dev, struct msm_vfe_stats_stream *stream_info,
1733         uint32_t pingpong_status, dma_addr_t paddr, uint32_t buf_sz)
1734 {
1735         void __iomem *vfe_base = vfe_dev->vfe_base;
1736         int vfe_idx = msm_isp_get_vfe_idx_for_stats_stream(vfe_dev,
1737                                 stream_info);
1738         uint32_t paddr32 = (paddr & 0xFFFFFFFF);
1739         int stats_idx;
1740
1741         stats_idx = STATS_IDX(stream_info->stream_handle[vfe_idx]);
1742         msm_camera_io_w(paddr32, vfe_base +
1743                 VFE44_STATS_PING_PONG_BASE(stats_idx, pingpong_status));
1744 }
1745
1746 static uint32_t msm_vfe44_stats_get_wm_mask(
1747         uint32_t irq_status0, uint32_t irq_status1)
1748 {
1749         return (irq_status0 >> 15) & 0x1FF;
1750 }
1751
1752 static uint32_t msm_vfe44_stats_get_comp_mask(
1753         uint32_t irq_status0, uint32_t irq_status1)
1754 {
1755         return (irq_status0 >> 29) & 0x3;
1756 }
1757
1758 static uint32_t msm_vfe44_stats_get_frame_id(
1759         struct vfe_device *vfe_dev)
1760 {
1761         return vfe_dev->axi_data.src_info[VFE_PIX_0].frame_id;
1762 }
1763
1764 static void msm_vfe44_get_error_mask(
1765         uint32_t *error_mask0, uint32_t *error_mask1)
1766 {
1767         *error_mask0 = 0x00000000;
1768         *error_mask1 = 0x01FFFEFF;
1769 }
1770
1771 static void msm_vfe44_get_overflow_mask(uint32_t *overflow_mask)
1772 {
1773         *overflow_mask = 0x00FFFE7E;
1774 }
1775
1776 static void msm_vfe44_get_rdi_wm_mask(struct vfe_device *vfe_dev,
1777         uint32_t *rdi_wm_mask)
1778 {
1779         *rdi_wm_mask = vfe_dev->axi_data.rdi_wm_mask;
1780 }
1781
1782 static void msm_vfe44_get_irq_mask(struct vfe_device *vfe_dev,
1783         uint32_t *irq0_mask, uint32_t *irq1_mask)
1784 {
1785         *irq0_mask = vfe_dev->irq0_mask;
1786         *irq1_mask = vfe_dev->irq1_mask;
1787 }
1788
1789 static void msm_vfe44_get_halt_restart_mask(uint32_t *irq0_mask,
1790         uint32_t *irq1_mask)
1791 {
1792         *irq0_mask = BIT(31);
1793         *irq1_mask = BIT(8);
1794 }
1795
1796 static struct msm_vfe_axi_hardware_info msm_vfe44_axi_hw_info = {
1797         .num_wm = 6,
1798         .num_comp_mask = 3,
1799         .num_rdi = 3,
1800         .num_rdi_master = 3,
1801         .min_wm_ub = 96,
1802         .scratch_buf_range = SZ_32M + SZ_4M,
1803 };
1804
1805 static struct msm_vfe_stats_hardware_info msm_vfe44_stats_hw_info = {
1806         .stats_capability_mask =
1807                 1 << MSM_ISP_STATS_BE | 1 << MSM_ISP_STATS_BF |
1808                 1 << MSM_ISP_STATS_BG | 1 << MSM_ISP_STATS_BHIST |
1809                 1 << MSM_ISP_STATS_AWB | 1 << MSM_ISP_STATS_IHIST |
1810                 1 << MSM_ISP_STATS_RS | 1 << MSM_ISP_STATS_CS |
1811                 1 << MSM_ISP_STATS_BF_SCALE,
1812         .stats_ping_pong_offset = stats_pingpong_offset_map,
1813         .num_stats_type = VFE44_NUM_STATS_TYPE,
1814         .num_stats_comp_mask = VFE44_NUM_STATS_COMP,
1815 };
1816
1817 struct msm_vfe_hardware_info vfe44_hw_info = {
1818         .num_iommu_ctx = 1,
1819         .num_iommu_secure_ctx = 1,
1820         .vfe_clk_idx = VFE44_CLK_IDX,
1821         .runtime_axi_update = 0,
1822         .min_ab = 100000000,
1823         .min_ib = 100000000,
1824         .vfe_ops = {
1825                 .irq_ops = {
1826                         .read_and_clear_irq_status =
1827                                 msm_vfe44_read_and_clear_irq_status,
1828                         .read_irq_status = msm_vfe44_read_irq_status,
1829                         .process_camif_irq = msm_vfe44_process_input_irq,
1830                         .process_reset_irq = msm_vfe44_process_reset_irq,
1831                         .process_halt_irq = msm_vfe44_process_halt_irq,
1832                         .process_reset_irq = msm_vfe44_process_reset_irq,
1833                         .process_reg_update = msm_vfe44_process_reg_update,
1834                         .process_axi_irq = msm_isp_process_axi_irq,
1835                         .process_stats_irq = msm_isp_process_stats_irq,
1836                         .process_epoch_irq = msm_vfe44_process_epoch_irq,
1837                         .config_irq = msm_vfe44_config_irq,
1838                         .preprocess_camif_irq = msm_isp47_preprocess_camif_irq,
1839                 },
1840                 .axi_ops = {
1841                         .reload_wm = msm_vfe44_axi_reload_wm,
1842                         .enable_wm = msm_vfe44_axi_enable_wm,
1843                         .cfg_io_format = msm_vfe44_cfg_io_format,
1844                         .cfg_comp_mask = msm_vfe44_axi_cfg_comp_mask,
1845                         .clear_comp_mask = msm_vfe44_axi_clear_comp_mask,
1846                         .cfg_wm_irq_mask = msm_vfe44_axi_cfg_wm_irq_mask,
1847                         .clear_wm_irq_mask = msm_vfe44_axi_clear_wm_irq_mask,
1848                         .cfg_framedrop = msm_vfe44_cfg_framedrop,
1849                         .clear_framedrop = msm_vfe44_clear_framedrop,
1850                         .cfg_wm_reg = msm_vfe44_axi_cfg_wm_reg,
1851                         .clear_wm_reg = msm_vfe44_axi_clear_wm_reg,
1852                         .cfg_wm_xbar_reg = msm_vfe44_axi_cfg_wm_xbar_reg,
1853                         .clear_wm_xbar_reg = msm_vfe44_axi_clear_wm_xbar_reg,
1854                         .cfg_ub = msm_vfe47_cfg_axi_ub,
1855                         .read_wm_ping_pong_addr =
1856                                 msm_vfe44_read_wm_ping_pong_addr,
1857                         .update_ping_pong_addr =
1858                                 msm_vfe44_update_ping_pong_addr,
1859                         .get_comp_mask = msm_vfe44_get_comp_mask,
1860                         .get_wm_mask = msm_vfe44_get_wm_mask,
1861                         .get_pingpong_status = msm_vfe44_get_pingpong_status,
1862                         .halt = msm_vfe44_axi_halt,
1863                         .restart = msm_vfe44_axi_restart,
1864                         .update_cgc_override =
1865                                 msm_vfe44_axi_update_cgc_override,
1866                         .ub_reg_offset = msm_vfe44_ub_reg_offset,
1867                         .get_ub_size = msm_vfe44_get_ub_size,
1868                 },
1869                 .core_ops = {
1870                         .reg_update = msm_vfe44_reg_update,
1871                         .cfg_input_mux = msm_vfe44_cfg_input_mux,
1872                         .update_camif_state = msm_vfe44_update_camif_state,
1873                         .start_fetch_eng = msm_vfe44_fetch_engine_start,
1874                         .cfg_rdi_reg = msm_vfe44_cfg_rdi_reg,
1875                         .reset_hw = msm_vfe44_reset_hardware,
1876                         .init_hw = msm_vfe47_init_hardware,
1877                         .init_hw_reg = msm_vfe44_init_hardware_reg,
1878                         .clear_status_reg = msm_vfe44_clear_status_reg,
1879                         .release_hw = msm_vfe47_release_hardware,
1880                         .get_error_mask = msm_vfe44_get_error_mask,
1881                         .get_overflow_mask = msm_vfe44_get_overflow_mask,
1882                         .get_rdi_wm_mask = msm_vfe44_get_rdi_wm_mask,
1883                         .get_irq_mask = msm_vfe44_get_irq_mask,
1884                         .get_halt_restart_mask =
1885                                 msm_vfe44_get_halt_restart_mask,
1886                         .process_error_status = msm_vfe44_process_error_status,
1887                         .is_module_cfg_lock_needed =
1888                                 msm_vfe44_is_module_cfg_lock_needed,
1889                         .ahb_clk_cfg = NULL,
1890                         .set_halt_restart_mask =
1891                                 msm_vfe44_set_halt_restart_mask,
1892                         .set_bus_err_ign_mask = NULL,
1893                         .get_bus_err_mask = NULL,
1894                 },
1895                 .stats_ops = {
1896                         .get_stats_idx = msm_vfe44_get_stats_idx,
1897                         .check_streams = msm_vfe44_stats_check_streams,
1898                         .cfg_comp_mask = msm_vfe44_stats_cfg_comp_mask,
1899                         .cfg_wm_irq_mask = msm_vfe44_stats_cfg_wm_irq_mask,
1900                         .clear_wm_irq_mask = msm_vfe44_stats_clear_wm_irq_mask,
1901                         .cfg_wm_reg = msm_vfe44_stats_cfg_wm_reg,
1902                         .clear_wm_reg = msm_vfe44_stats_clear_wm_reg,
1903                         .cfg_ub = msm_vfe44_stats_cfg_ub,
1904                         .enable_module = msm_vfe44_stats_enable_module,
1905                         .update_ping_pong_addr =
1906                                 msm_vfe44_stats_update_ping_pong_addr,
1907                         .get_comp_mask = msm_vfe44_stats_get_comp_mask,
1908                         .get_wm_mask = msm_vfe44_stats_get_wm_mask,
1909                         .get_frame_id = msm_vfe44_stats_get_frame_id,
1910                         .get_pingpong_status = msm_vfe44_get_pingpong_status,
1911                         .update_cgc_override =
1912                                 msm_vfe44_stats_update_cgc_override,
1913                         .enable_stats_wm = NULL,
1914                 },
1915                 .platform_ops = {
1916                         .get_platform_data = msm_vfe47_get_platform_data,
1917                         .enable_regulators = msm_vfe47_enable_regulators,
1918                         .get_regulators = msm_vfe47_get_regulators,
1919                         .put_regulators = msm_vfe47_put_regulators,
1920                         .enable_clks = msm_vfe47_enable_clks,
1921                         .get_clks = msm_vfe47_get_clks,
1922                         .put_clks = msm_vfe47_put_clks,
1923                         .get_clk_rates = msm_vfe47_get_clk_rates,
1924                         .get_max_clk_rate = msm_vfe47_get_max_clk_rate,
1925                         .set_clk_rate = msm_vfe47_set_clk_rate,
1926                         .init_bw_mgr = msm_vfe47_init_bandwidth_mgr,
1927                         .deinit_bw_mgr = msm_vfe47_deinit_bandwidth_mgr,
1928                         .update_bw = msm_vfe47_update_bandwidth,
1929                 }
1930         },
1931         .dmi_reg_offset = 0x918,
1932         .axi_hw_info = &msm_vfe44_axi_hw_info,
1933         .stats_hw_info = &msm_vfe44_stats_hw_info,
1934         .regulator_names = {"vdd"},
1935 };
1936 EXPORT_SYMBOL(vfe44_hw_info);
1937
1938 static const struct of_device_id msm_vfe44_dt_match[] = {
1939         {
1940                 .compatible = "qcom,vfe44",
1941                 .data = &vfe44_hw_info,
1942         },
1943         {}
1944 };
1945
1946 MODULE_DEVICE_TABLE(of, msm_vfe44_dt_match);
1947
1948 static struct platform_driver vfe44_driver = {
1949         .probe = vfe_hw_probe,
1950         .driver = {
1951                 .name = "msm_vfe44",
1952                 .owner = THIS_MODULE,
1953                 .of_match_table = msm_vfe44_dt_match,
1954         },
1955 };
1956
1957 static int __init msm_vfe44_init_module(void)
1958 {
1959         return platform_driver_register(&vfe44_driver);
1960 }
1961
1962 static void __exit msm_vfe44_exit_module(void)
1963 {
1964         platform_driver_unregister(&vfe44_driver);
1965 }
1966
1967 module_init(msm_vfe44_init_module);
1968 module_exit(msm_vfe44_exit_module);
1969 MODULE_DESCRIPTION("MSM VFE44 driver");
1970 MODULE_LICENSE("GPL v2");
1971