blob: 0689189937750c43adc00e1c37dad2f29c64c61b [file] [log] [blame]
Terence Hampsonaeb793e2012-05-11 11:41:16 -04001/* Copyright (c) 2012, Code Aurora Forum. 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/kernel.h>
14#include <linux/init.h>
15#include <linux/sched.h>
16#include <linux/kthread.h>
17#include <linux/freezer.h>
Terence Hampsonaeb793e2012-05-11 11:41:16 -040018#include <linux/clk.h>
Terence Hampsonf1b7b582012-10-16 11:49:24 -040019#include <linux/io.h>
20
21#include <mach/camera.h>
22#include <mach/clk.h>
Terence Hampsonaeb793e2012-05-11 11:41:16 -040023
Terence Hampson98d11802012-06-06 18:18:43 -040024#include <media/v4l2-event.h>
Terence Hampsonaeb793e2012-05-11 11:41:16 -040025#include <media/vcap_v4l2.h>
26#include <media/vcap_fmt.h>
Terence Hampsonf1b7b582012-10-16 11:49:24 -040027
Terence Hampsonaeb793e2012-05-11 11:41:16 -040028#include "vcap_vp.h"
29
Terence Hampsonaeb793e2012-05-11 11:41:16 -040030void config_nr_buffer(struct vcap_client_data *c_data,
31 struct vcap_buffer *buf)
32{
33 struct vcap_dev *dev = c_data->dev;
34 int size = c_data->vp_in_fmt.height * c_data->vp_in_fmt.width;
35
36 writel_relaxed(buf->paddr, VCAP_VP_NR_T2_Y_BASE_ADDR);
37 writel_relaxed(buf->paddr + size, VCAP_VP_NR_T2_C_BASE_ADDR);
38}
39
40void config_in_buffer(struct vcap_client_data *c_data,
41 struct vcap_buffer *buf)
42{
43 struct vcap_dev *dev = c_data->dev;
44 int size = c_data->vp_in_fmt.height * c_data->vp_in_fmt.width;
45
46 writel_relaxed(buf->paddr, VCAP_VP_T2_Y_BASE_ADDR);
47 writel_relaxed(buf->paddr + size, VCAP_VP_T2_C_BASE_ADDR);
48}
49
50void config_out_buffer(struct vcap_client_data *c_data,
51 struct vcap_buffer *buf)
52{
53 struct vcap_dev *dev = c_data->dev;
54 int size;
55 size = c_data->vp_out_fmt.height * c_data->vp_out_fmt.width;
56 writel_relaxed(buf->paddr, VCAP_VP_OUT_Y_BASE_ADDR);
57 writel_relaxed(buf->paddr + size, VCAP_VP_OUT_C_BASE_ADDR);
58}
59
60int vp_setup_buffers(struct vcap_client_data *c_data)
61{
62 struct vp_action *vp_act;
63 struct vcap_dev *dev;
64 unsigned long flags = 0;
65
66 if (!c_data->streaming)
67 return -ENOEXEC;
68 dev = c_data->dev;
Terence Hampsonc4aa4c02012-08-31 04:49:21 -040069 pr_debug("VP: Start setup buffers\n");
Terence Hampsonaeb793e2012-05-11 11:41:16 -040070
Terence Hampsonb128b982012-08-16 14:41:19 -040071 if (dev->vp_shutdown) {
Terence Hampsonc4aa4c02012-08-31 04:49:21 -040072 pr_debug("%s: VP shutting down, no buf setup\n",
Terence Hampsonb128b982012-08-16 14:41:19 -040073 __func__);
74 return -EPERM;
75 }
76
Terence Hampsonaeb793e2012-05-11 11:41:16 -040077 /* No need to verify vp_client is not NULL caller does so */
Terence Hampson2cba63f2012-08-21 10:54:38 -040078 vp_act = &dev->vp_client->vp_action;
Terence Hampsonaeb793e2012-05-11 11:41:16 -040079
80 spin_lock_irqsave(&dev->vp_client->cap_slock, flags);
81 if (list_empty(&vp_act->in_active)) {
82 spin_unlock_irqrestore(&dev->vp_client->cap_slock, flags);
Terence Hampsonc4aa4c02012-08-31 04:49:21 -040083 pr_debug("%s: VP We have no more input buffers\n",
Terence Hampsonaeb793e2012-05-11 11:41:16 -040084 __func__);
85 return -EAGAIN;
86 }
87
88 if (list_empty(&vp_act->out_active)) {
89 spin_unlock_irqrestore(&dev->vp_client->cap_slock,
90 flags);
Terence Hampsonc4aa4c02012-08-31 04:49:21 -040091 pr_debug("%s: VP We have no more output buffers\n",
Terence Hampsonaeb793e2012-05-11 11:41:16 -040092 __func__);
93 return -EAGAIN;
94 }
95
96 vp_act->bufT2 = list_entry(vp_act->in_active.next,
97 struct vcap_buffer, list);
98 list_del(&vp_act->bufT2->list);
99
100 vp_act->bufOut = list_entry(vp_act->out_active.next,
101 struct vcap_buffer, list);
102 list_del(&vp_act->bufOut->list);
103 spin_unlock_irqrestore(&dev->vp_client->cap_slock, flags);
104
105 config_in_buffer(c_data, vp_act->bufT2);
106 config_out_buffer(c_data, vp_act->bufOut);
107 return 0;
108}
109
110static void mov_buf_to_vc(struct work_struct *work)
111{
112 struct vp_work_t *vp_work = container_of(work, struct vp_work_t, work);
113 struct v4l2_buffer p;
114 struct vb2_buffer *vb_vc;
115 struct vcap_buffer *buf_vc;
116 struct vb2_buffer *vb_vp;
117 struct vcap_buffer *buf_vp;
118 int rc;
119
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400120 p.memory = V4L2_MEMORY_USERPTR;
121
122 /* This loop exits when there is no more buffers left */
123 while (1) {
Terence Hampsona6c96482012-07-06 17:53:09 -0400124 p.type = V4L2_BUF_TYPE_INTERLACED_IN_DECODER;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400125 if (!vp_work->cd->streaming)
126 return;
Terence Hampsona6c96482012-07-06 17:53:09 -0400127 rc = vcvp_dqbuf(&vp_work->cd->vp_in_vidq, &p);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400128 if (rc < 0)
129 return;
130
131 vb_vc = vp_work->cd->vc_vidq.bufs[p.index];
132 if (NULL == vb_vc) {
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400133 pr_debug("%s: buffer is NULL\n", __func__);
Terence Hampsona6c96482012-07-06 17:53:09 -0400134 vcvp_qbuf(&vp_work->cd->vp_in_vidq, &p);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400135 return;
136 }
137 buf_vc = container_of(vb_vc, struct vcap_buffer, vb);
138
139 vb_vp = vp_work->cd->vp_in_vidq.bufs[p.index];
140 if (NULL == vb_vp) {
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400141 pr_debug("%s: buffer is NULL\n", __func__);
Terence Hampsona6c96482012-07-06 17:53:09 -0400142 vcvp_qbuf(&vp_work->cd->vp_in_vidq, &p);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400143 return;
144 }
145 buf_vp = container_of(vb_vp, struct vcap_buffer, vb);
146 buf_vc->ion_handle = buf_vp->ion_handle;
147 buf_vc->paddr = buf_vp->paddr;
148 buf_vp->ion_handle = NULL;
149 buf_vp->paddr = 0;
150
151 p.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
152 /* This call should not fail */
Terence Hampsona6c96482012-07-06 17:53:09 -0400153 rc = vcvp_qbuf(&vp_work->cd->vc_vidq, &p);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400154 if (rc < 0) {
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400155 pr_err("%s: qbuf to vc failed\n", __func__);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400156 buf_vp->ion_handle = buf_vc->ion_handle;
157 buf_vp->paddr = buf_vc->paddr;
158 buf_vc->ion_handle = NULL;
159 buf_vc->paddr = 0;
160 p.type = V4L2_BUF_TYPE_INTERLACED_IN_DECODER;
Terence Hampsona6c96482012-07-06 17:53:09 -0400161 vcvp_qbuf(&vp_work->cd->vp_in_vidq, &p);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400162 }
163 }
164}
165
Terence Hampson616762d2012-08-16 17:47:44 -0400166void update_nr_value(struct vcap_dev *dev)
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400167{
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400168 struct nr_param *par;
Terence Hampson6a744e32012-12-07 15:50:32 -0500169 uint32_t val = 0;
Terence Hampson616762d2012-08-16 17:47:44 -0400170 par = &dev->nr_param;
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400171 if (par->mode == NR_MANUAL) {
172 writel_relaxed(par->window << 24 | par->decay_ratio << 20,
173 VCAP_VP_NR_CONFIG);
Terence Hampson6a744e32012-12-07 15:50:32 -0500174 if (par->threshold)
175 val = VP_NR_DYNAMIC_THRESHOLD;
176 writel_relaxed(val |
177 par->luma.max_blend_ratio << 24 |
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400178 par->luma.scale_diff_ratio << 12 |
179 par->luma.diff_limit_ratio << 8 |
180 par->luma.scale_motion_ratio << 4 |
181 par->luma.blend_limit_ratio << 0,
182 VCAP_VP_NR_LUMA_CONFIG);
Terence Hampson6a744e32012-12-07 15:50:32 -0500183 writel_relaxed(val |
184 par->chroma.max_blend_ratio << 24 |
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400185 par->chroma.scale_diff_ratio << 12 |
186 par->chroma.diff_limit_ratio << 8 |
187 par->chroma.scale_motion_ratio << 4 |
188 par->chroma.blend_limit_ratio << 0,
189 VCAP_VP_NR_CHROMA_CONFIG);
190 }
Terence Hampson616762d2012-08-16 17:47:44 -0400191 dev->nr_update = false;
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400192}
193
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400194static void vp_wq_fnc(struct work_struct *work)
195{
196 struct vp_work_t *vp_work = container_of(work, struct vp_work_t, work);
197 struct vcap_dev *dev;
198 struct vp_action *vp_act;
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400199 struct timeval tv;
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400200 unsigned long flags = 0;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400201 uint32_t irq;
202 int rc;
Terence Hampson9c4f8a22012-09-12 10:25:50 -0400203 bool top_field = 0;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400204
205 if (vp_work && vp_work->cd && vp_work->cd->dev)
206 dev = vp_work->cd->dev;
207 else
208 return;
209
Terence Hampson2cba63f2012-08-21 10:54:38 -0400210 vp_act = &dev->vp_client->vp_action;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400211
212 rc = readl_relaxed(VCAP_OFFSET(0x048));
213 while (!(rc & 0x00000100))
214 rc = readl_relaxed(VCAP_OFFSET(0x048));
215
Terence Hampsonad33c512012-07-16 17:50:00 -0400216 irq = readl_relaxed(VCAP_VP_INT_STATUS);
217
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400218 writel_relaxed(0x00000000, VCAP_VP_BAL_VMOTION_STATE);
219 writel_relaxed(0x40000000, VCAP_VP_REDUCT_AVG_MOTION2);
220
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400221 spin_lock_irqsave(&dev->vp_client->cap_slock, flags);
Terence Hampson616762d2012-08-16 17:47:44 -0400222 if (dev->nr_update == true)
223 update_nr_value(dev);
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400224 spin_unlock_irqrestore(&dev->vp_client->cap_slock, flags);
225
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400226 /* Queue the done buffers */
227 if (vp_act->vp_state == VP_NORMAL &&
228 vp_act->bufNR.nr_pos != TM1_BUF) {
229 vb2_buffer_done(&vp_act->bufTm1->vb, VB2_BUF_STATE_DONE);
230 if (vp_work->cd->op_mode == VC_AND_VP_VCAP_OP)
231 queue_work(dev->vcap_wq, &dev->vp_to_vc_work.work);
232 }
233
Terence Hampson3af95c02012-09-18 16:43:38 -0400234 if (vp_act->bufT0 != NULL && vp_act->vp_state == VP_NORMAL) {
235 vp_act->bufOut->vb.v4l2_buf.timestamp =
236 vp_act->bufT0->vb.v4l2_buf.timestamp;
237 }
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400238 vb2_buffer_done(&vp_act->bufOut->vb, VB2_BUF_STATE_DONE);
239
240 /* Cycle to next state */
241 if (vp_act->vp_state != VP_NORMAL)
242 vp_act->vp_state++;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400243
244 /* Cycle Buffers*/
Terence Hampson616762d2012-08-16 17:47:44 -0400245 if (dev->nr_param.mode) {
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400246 if (vp_act->bufNR.nr_pos == TM1_BUF)
247 vp_act->bufNR.nr_pos = BUF_NOT_IN_USE;
248
249 if (vp_act->bufNR.nr_pos != BUF_NOT_IN_USE)
250 vp_act->bufNR.nr_pos++;
251
252 vp_act->bufTm1 = vp_act->bufT0;
253 vp_act->bufT0 = vp_act->bufT1;
254 vp_act->bufT1 = vp_act->bufNRT2;
255 vp_act->bufNRT2 = vp_act->bufT2;
256 config_nr_buffer(vp_work->cd, vp_act->bufNRT2);
257 } else {
258 vp_act->bufTm1 = vp_act->bufT0;
259 vp_act->bufT0 = vp_act->bufT1;
260 vp_act->bufT1 = vp_act->bufT2;
261 }
262
263 rc = vp_setup_buffers(vp_work->cd);
264 if (rc < 0) {
265 /* setup_buf failed because we are waiting for buffers */
266 writel_relaxed(0x00000000, VCAP_VP_INTERRUPT_ENABLE);
267 writel_iowmb(irq, VCAP_VP_INT_CLEAR);
268 atomic_set(&dev->vp_enabled, 0);
Terence Hampsonb128b982012-08-16 14:41:19 -0400269 if (dev->vp_shutdown)
270 wake_up(&dev->vp_dummy_waitq);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400271 return;
272 }
273
274 /* Config VP */
Terence Hampsonaa64d3a2012-10-29 13:05:37 -0400275 if (vp_act->bufT2->vb.v4l2_buf.field == V4L2_FIELD_BOTTOM)
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400276 top_field = 1;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400277
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400278 writel_iowmb(0x00000000 | top_field, VCAP_VP_CTRL);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400279 writel_iowmb(0x00010000 | top_field, VCAP_VP_CTRL);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400280 enable_irq(dev->vpirq->start);
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400281
282 do_gettimeofday(&tv);
283 dev->dbg_p.vp_timestamp = (uint32_t) (tv.tv_sec * VCAP_USEC +
284 tv.tv_usec);
285
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400286 writel_iowmb(irq, VCAP_VP_INT_CLEAR);
287}
288
289irqreturn_t vp_handler(struct vcap_dev *dev)
290{
291 struct vcap_client_data *c_data;
292 struct vp_action *vp_act;
Terence Hampson98d11802012-06-06 18:18:43 -0400293 struct v4l2_event v4l2_evt;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400294 uint32_t irq;
295 int rc;
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400296 struct timeval tv;
297 uint32_t new_ts;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400298
299 irq = readl_relaxed(VCAP_VP_INT_STATUS);
Terence Hampson54d3a6d2012-07-10 14:05:35 -0400300 if (dev->vp_dummy_event == true) {
301 writel_relaxed(irq, VCAP_VP_INT_CLEAR);
302 dev->vp_dummy_complete = true;
303 wake_up(&dev->vp_dummy_waitq);
304 return IRQ_HANDLED;
305 }
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400306
Terence Hampson98d11802012-06-06 18:18:43 -0400307 if (irq & 0x02000000) {
308 v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
309 VCAP_VP_REG_R_ERR_EVENT;
310 v4l2_event_queue(dev->vfd, &v4l2_evt);
311 }
312 if (irq & 0x01000000) {
313 v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
Terence Hampsonf3cd9a12012-09-21 11:18:02 -0400314 VCAP_VP_REG_W_ERR_EVENT;
Terence Hampson98d11802012-06-06 18:18:43 -0400315 v4l2_event_queue(dev->vfd, &v4l2_evt);
316 }
317 if (irq & 0x00020000) {
318 v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
319 VCAP_VP_IN_HEIGHT_ERR_EVENT;
320 v4l2_event_queue(dev->vfd, &v4l2_evt);
321 }
322 if (irq & 0x00010000) {
323 v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
324 VCAP_VP_IN_WIDTH_ERR_EVENT;
325 v4l2_event_queue(dev->vfd, &v4l2_evt);
326 }
327
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400328 pr_debug("%s: irq=0x%08x\n", __func__, irq);
Terence Hampson72452632012-08-13 12:32:24 -0400329 if (!(irq & (VP_PIC_DONE | VP_MODE_CHANGE))) {
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400330 writel_relaxed(irq, VCAP_VP_INT_CLEAR);
331 pr_err("VP IRQ shows some error\n");
332 return IRQ_HANDLED;
333 }
334
335 if (dev->vp_client == NULL) {
336 writel_relaxed(irq, VCAP_VP_INT_CLEAR);
337 pr_err("VC: There is no active vp client\n");
338 return IRQ_HANDLED;
339 }
340
Terence Hampson2cba63f2012-08-21 10:54:38 -0400341 vp_act = &dev->vp_client->vp_action;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400342 c_data = dev->vp_client;
343
344 if (vp_act->vp_state == VP_UNKNOWN) {
345 writel_relaxed(irq, VCAP_VP_INT_CLEAR);
346 pr_err("%s: VP is in an unknown state\n",
347 __func__);
348 return -EAGAIN;
349 }
350
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400351 do_gettimeofday(&tv);
352 new_ts = (uint32_t) (tv.tv_sec * VCAP_USEC +
353 tv.tv_usec);
354 if (new_ts > dev->dbg_p.vp_timestamp) {
355 dev->dbg_p.vp_ewma = ((new_ts - dev->dbg_p.vp_timestamp) /
356 10 + (dev->dbg_p.vp_ewma / 10 * 9));
357 }
358
359 dev->dbg_p.vp_timestamp = (uint32_t) (tv.tv_sec * VCAP_USEC +
360 tv.tv_usec);
361
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400362 INIT_WORK(&dev->vp_work.work, vp_wq_fnc);
363 dev->vp_work.cd = c_data;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400364 rc = queue_work(dev->vcap_wq, &dev->vp_work.work);
365
366 disable_irq_nosync(dev->vpirq->start);
367 return IRQ_HANDLED;
368}
369
Terence Hampsonb128b982012-08-16 14:41:19 -0400370int vp_sw_reset(struct vcap_dev *dev)
371{
372 int timeout;
373 writel_iowmb(0x00000010, VCAP_SW_RESET_REQ);
374 timeout = 10000;
375 while (1) {
376 if (!(readl_relaxed(VCAP_SW_RESET_STATUS) & 0x10))
377 break;
378 timeout--;
379 if (timeout == 0) {
380 /* This should not happen */
381 pr_err("VP is not resetting properly\n");
382 writel_iowmb(0x00000000, VCAP_SW_RESET_REQ);
383 return -EINVAL;
384 }
385 }
386 return 0;
387}
388
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400389void vp_stop_capture(struct vcap_client_data *c_data)
390{
391 struct vcap_dev *dev = c_data->dev;
Terence Hampsonb128b982012-08-16 14:41:19 -0400392 int rc;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400393
Terence Hampsonb128b982012-08-16 14:41:19 -0400394 dev->vp_shutdown = true;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400395 flush_workqueue(dev->vcap_wq);
396
Terence Hampsonb128b982012-08-16 14:41:19 -0400397 if (atomic_read(&dev->vp_enabled) == 1) {
398 rc = wait_event_interruptible_timeout(dev->vp_dummy_waitq,
399 !atomic_read(&dev->vp_enabled),
400 msecs_to_jiffies(50));
401 if (rc == 0 && atomic_read(&dev->vp_enabled) == 1) {
402 /* This should not happen, if it does hw is stuck */
Terence Hampsonf1b7b582012-10-16 11:49:24 -0400403 disable_irq_nosync(dev->vpirq->start);
Terence Hampson14600e52012-11-07 17:16:27 -0500404 atomic_set(&dev->vp_enabled, 0);
Terence Hampsonb128b982012-08-16 14:41:19 -0400405 pr_err("%s: VP Timeout and VP still running\n",
406 __func__);
407 }
408 }
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400409
Terence Hampsonb128b982012-08-16 14:41:19 -0400410 vp_sw_reset(dev);
411 dev->vp_shutdown = false;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400412}
413
414int config_vp_format(struct vcap_client_data *c_data)
415{
416 struct vcap_dev *dev = c_data->dev;
Terence Hampsonb128b982012-08-16 14:41:19 -0400417 int rc;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400418
419 INIT_WORK(&dev->vp_to_vc_work.work, mov_buf_to_vc);
420 dev->vp_to_vc_work.cd = c_data;
421
422 /* SW restart VP */
Terence Hampsonb128b982012-08-16 14:41:19 -0400423 rc = vp_sw_reset(dev);
424 if (rc < 0)
425 return rc;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400426
427 /* Film Mode related settings */
428 writel_iowmb(0x00000000, VCAP_VP_FILM_PROJECTION_T0);
429 writel_relaxed(0x00000000, VCAP_VP_FILM_PROJECTION_T2);
430 writel_relaxed(0x00000000, VCAP_VP_FILM_PAST_MAX_PROJ);
431 writel_relaxed(0x00000000, VCAP_VP_FILM_PAST_MIN_PROJ);
432 writel_relaxed(0x00000000, VCAP_VP_FILM_SEQUENCE_HIST);
433 writel_relaxed(0x00000000, VCAP_VP_FILM_MODE_STATE);
434
435 writel_relaxed(0x00000000, VCAP_VP_BAL_VMOTION_STATE);
436 writel_relaxed(0x00000010, VCAP_VP_REDUCT_AVG_MOTION);
437 writel_relaxed(0x40000000, VCAP_VP_REDUCT_AVG_MOTION2);
438 writel_relaxed(0x40000000, VCAP_VP_NR_AVG_LUMA);
439 writel_relaxed(0x40000000, VCAP_VP_NR_AVG_CHROMA);
440 writel_relaxed(0x40000000, VCAP_VP_NR_CTRL_LUMA);
441 writel_relaxed(0x40000000, VCAP_VP_NR_CTRL_CHROMA);
442 writel_relaxed(0x00000000, VCAP_VP_BAL_AVG_BLEND);
443 writel_relaxed(0x00000000, VCAP_VP_VMOTION_HIST);
444 writel_relaxed(0x05047D19, VCAP_VP_FILM_ANALYSIS_CONFIG);
445 writel_relaxed(0x20260200, VCAP_VP_FILM_STATE_CONFIG);
446 writel_relaxed(0x23A60114, VCAP_VP_FVM_CONFIG);
447 writel_relaxed(0x03043210, VCAP_VP_FILM_ANALYSIS_CONFIG2);
448 writel_relaxed(0x04DB7A51, VCAP_VP_MIXED_ANALYSIS_CONFIG);
449 writel_relaxed(0x14224916, VCAP_VP_SPATIAL_CONFIG);
450 writel_relaxed(0x83270400, VCAP_VP_SPATIAL_CONFIG2);
451 writel_relaxed(0x0F000F92, VCAP_VP_SPATIAL_CONFIG3);
452 writel_relaxed(0x00000000, VCAP_VP_TEMPORAL_CONFIG);
453 writel_relaxed(0x00000000, VCAP_VP_PIXEL_DIFF_CONFIG);
454 writel_relaxed(0x0C090511, VCAP_VP_H_FREQ_CONFIG);
455 writel_relaxed(0x0A000000, VCAP_VP_NR_CONFIG);
456 writel_relaxed(0x008F4149, VCAP_VP_NR_LUMA_CONFIG);
457 writel_relaxed(0x008F4149, VCAP_VP_NR_CHROMA_CONFIG);
458 writel_relaxed(0x43C0FD0C, VCAP_VP_BAL_CONFIG);
459 writel_relaxed(0x00000255, VCAP_VP_BAL_MOTION_CONFIG);
460 writel_relaxed(0x24154252, VCAP_VP_BAL_LIGHT_COMB);
461 writel_relaxed(0x10024414, VCAP_VP_BAL_VMOTION_CONFIG);
462 writel_relaxed(0x00000002, VCAP_VP_NR_CONFIG2);
463 writel_relaxed((c_data->vp_out_fmt.height-1)<<16 |
464 (c_data->vp_out_fmt.width - 1), VCAP_VP_FRAME_SIZE);
465 writel_relaxed(0x00000000, VCAP_VP_SPLIT_SCRN_CTRL);
466
467 return 0;
468}
469
470int init_motion_buf(struct vcap_client_data *c_data)
471{
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400472 int rc;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400473 struct vcap_dev *dev = c_data->dev;
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400474 struct ion_handle *handle = NULL;
Terence Hampson8d1c3272012-07-27 16:36:31 -0400475 unsigned long paddr, len, ionflag = 0;
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400476 void *vaddr;
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400477 size_t size = ((c_data->vp_out_fmt.width + 63) >> 6) *
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400478 ((c_data->vp_out_fmt.height + 7) >> 3) * 16;
479
Terence Hampson2cba63f2012-08-21 10:54:38 -0400480 if (c_data->vp_action.motionHandle) {
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400481 pr_err("Motion buffer has already been created");
482 return -ENOEXEC;
483 }
484
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400485 handle = ion_alloc(dev->ion_client, size, SZ_4K,
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700486 ION_HEAP(ION_CP_MM_HEAP_ID), 0);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400487 if (IS_ERR_OR_NULL(handle)) {
488 pr_err("%s: ion_alloc failed\n", __func__);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400489 return -ENOMEM;
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400490 }
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400491
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400492 rc = ion_handle_get_flags(dev->ion_client, handle, &ionflag);
493 if (rc) {
494 pr_err("%s: get flags ion handle failed\n", __func__);
495 ion_free(dev->ion_client, handle);
496 return rc;
497 }
498
Mitchel Humpherys911b4b72012-09-12 14:42:50 -0700499 vaddr = ion_map_kernel(dev->ion_client, handle);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400500 if (IS_ERR(vaddr)) {
501 pr_err("%s: Map motion buffer failed\n", __func__);
502 ion_free(dev->ion_client, handle);
503 rc = -ENOMEM;
504 return rc;
505 }
506
507 memset(vaddr, 0, size);
Terence Hampson8d1c3272012-07-27 16:36:31 -0400508 ion_unmap_kernel(dev->ion_client, handle);
509
510 rc = ion_map_iommu(dev->ion_client, handle,
511 dev->domain_num, 0, SZ_4K, 0, &paddr, &len,
512 0, 0);
513 if (rc < 0) {
514 pr_err("%s: map_iommu failed\n", __func__);
515 ion_free(dev->ion_client, handle);
516 return rc;
517 }
518
Terence Hampson2cba63f2012-08-21 10:54:38 -0400519 c_data->vp_action.motionHandle = handle;
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400520
521 vaddr = NULL;
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400522
523 writel_iowmb(paddr, VCAP_VP_MOTION_EST_ADDR);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400524 return 0;
525}
526
527void deinit_motion_buf(struct vcap_client_data *c_data)
528{
529 struct vcap_dev *dev = c_data->dev;
Terence Hampson2cba63f2012-08-21 10:54:38 -0400530 if (!c_data->vp_action.motionHandle) {
Terence Hampsonad33c512012-07-16 17:50:00 -0400531 pr_err("Motion buffer has not been created");
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400532 return;
533 }
534
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400535 writel_iowmb(0x00000000, VCAP_VP_MOTION_EST_ADDR);
Terence Hampson8d1c3272012-07-27 16:36:31 -0400536 ion_unmap_iommu(dev->ion_client, c_data->vp_action.motionHandle,
537 dev->domain_num, 0);
Terence Hampson2cba63f2012-08-21 10:54:38 -0400538 ion_free(dev->ion_client, c_data->vp_action.motionHandle);
539 c_data->vp_action.motionHandle = NULL;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400540 return;
541}
542
543int init_nr_buf(struct vcap_client_data *c_data)
544{
545 struct vcap_dev *dev = c_data->dev;
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400546 struct ion_handle *handle = NULL;
Terence Hampson8d1c3272012-07-27 16:36:31 -0400547 size_t frame_size, tot_size;
548 unsigned long paddr, len;
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400549 int rc;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400550
Terence Hampson2cba63f2012-08-21 10:54:38 -0400551 if (c_data->vp_action.bufNR.nr_handle) {
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400552 pr_err("NR buffer has already been created");
553 return -ENOEXEC;
554 }
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400555
556 frame_size = c_data->vp_in_fmt.width * c_data->vp_in_fmt.height;
557 if (c_data->vp_in_fmt.pixfmt == V4L2_PIX_FMT_NV16)
558 tot_size = frame_size * 2;
559 else
560 tot_size = frame_size / 2 * 3;
561
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400562 handle = ion_alloc(dev->ion_client, tot_size, SZ_4K,
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700563 ION_HEAP(ION_CP_MM_HEAP_ID), 0);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400564 if (IS_ERR_OR_NULL(handle)) {
565 pr_err("%s: ion_alloc failed\n", __func__);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400566 return -ENOMEM;
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400567 }
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400568
Terence Hampson8d1c3272012-07-27 16:36:31 -0400569 rc = ion_map_iommu(dev->ion_client, handle,
570 dev->domain_num, 0, SZ_4K, 0, &paddr, &len,
571 0, 0);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400572 if (rc < 0) {
Terence Hampson8d1c3272012-07-27 16:36:31 -0400573 pr_err("%s: map_iommu failed\n", __func__);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400574 ion_free(dev->ion_client, handle);
575 return rc;
576 }
577
Terence Hampson2cba63f2012-08-21 10:54:38 -0400578 c_data->vp_action.bufNR.nr_handle = handle;
Terence Hampson616762d2012-08-16 17:47:44 -0400579 update_nr_value(dev);
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400580
Terence Hampson2cba63f2012-08-21 10:54:38 -0400581 c_data->vp_action.bufNR.paddr = paddr;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400582 rc = readl_relaxed(VCAP_VP_NR_CONFIG2);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400583 rc |= (((c_data->vp_out_fmt.width / 16) << 20) | 0x1);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400584 writel_relaxed(rc, VCAP_VP_NR_CONFIG2);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400585 writel_relaxed(paddr, VCAP_VP_NR_T2_Y_BASE_ADDR);
586 writel_relaxed(paddr + frame_size, VCAP_VP_NR_T2_C_BASE_ADDR);
Terence Hampson2cba63f2012-08-21 10:54:38 -0400587 c_data->vp_action.bufNR.nr_pos = NRT2_BUF;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400588 return 0;
589}
590
591void deinit_nr_buf(struct vcap_client_data *c_data)
592{
593 struct vcap_dev *dev = c_data->dev;
594 struct nr_buffer *buf;
595 uint32_t rc;
596
Terence Hampson2cba63f2012-08-21 10:54:38 -0400597 if (!c_data->vp_action.bufNR.nr_handle) {
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400598 pr_err("NR buffer has not been created");
599 return;
600 }
Terence Hampson2cba63f2012-08-21 10:54:38 -0400601 buf = &c_data->vp_action.bufNR;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400602
603 rc = readl_relaxed(VCAP_VP_NR_CONFIG2);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400604 rc &= !(0x0FF00001);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400605 writel_relaxed(rc, VCAP_VP_NR_CONFIG2);
606
Terence Hampson8d1c3272012-07-27 16:36:31 -0400607 ion_unmap_iommu(dev->ion_client, buf->nr_handle, dev->domain_num, 0);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400608 ion_free(dev->ion_client, buf->nr_handle);
609 buf->nr_handle = NULL;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400610 buf->paddr = 0;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400611 return;
612}
613
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400614int nr_s_param(struct vcap_client_data *c_data, struct nr_param *param)
615{
616 if (param->mode != NR_MANUAL)
617 return 0;
618
619 /* Verify values in range */
Terence Hampson616762d2012-08-16 17:47:44 -0400620 if (param->window > VP_NR_MAX_WINDOW)
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400621 return -EINVAL;
Terence Hampson616762d2012-08-16 17:47:44 -0400622 if (param->luma.max_blend_ratio > VP_NR_MAX_RATIO)
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400623 return -EINVAL;
Terence Hampson616762d2012-08-16 17:47:44 -0400624 if (param->luma.scale_diff_ratio > VP_NR_MAX_RATIO)
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400625 return -EINVAL;
Terence Hampson616762d2012-08-16 17:47:44 -0400626 if (param->luma.diff_limit_ratio > VP_NR_MAX_RATIO)
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400627 return -EINVAL;
Terence Hampson616762d2012-08-16 17:47:44 -0400628 if (param->luma.scale_motion_ratio > VP_NR_MAX_RATIO)
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400629 return -EINVAL;
Terence Hampson616762d2012-08-16 17:47:44 -0400630 if (param->luma.blend_limit_ratio > VP_NR_MAX_RATIO)
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400631 return -EINVAL;
Terence Hampson616762d2012-08-16 17:47:44 -0400632 if (param->chroma.max_blend_ratio > VP_NR_MAX_RATIO)
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400633 return -EINVAL;
Terence Hampson616762d2012-08-16 17:47:44 -0400634 if (param->chroma.scale_diff_ratio > VP_NR_MAX_RATIO)
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400635 return -EINVAL;
Terence Hampson616762d2012-08-16 17:47:44 -0400636 if (param->chroma.diff_limit_ratio > VP_NR_MAX_RATIO)
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400637 return -EINVAL;
Terence Hampson616762d2012-08-16 17:47:44 -0400638 if (param->chroma.scale_motion_ratio > VP_NR_MAX_RATIO)
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400639 return -EINVAL;
Terence Hampson616762d2012-08-16 17:47:44 -0400640 if (param->chroma.blend_limit_ratio > VP_NR_MAX_RATIO)
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400641 return -EINVAL;
642 return 0;
643}
644
645void nr_g_param(struct vcap_client_data *c_data, struct nr_param *param)
646{
647 struct vcap_dev *dev = c_data->dev;
648 uint32_t rc;
649 rc = readl_relaxed(VCAP_VP_NR_CONFIG);
650 param->window = BITS_VALUE(rc, 24, 4);
651 param->decay_ratio = BITS_VALUE(rc, 20, 3);
652
653 rc = readl_relaxed(VCAP_VP_NR_LUMA_CONFIG);
Terence Hampson6a744e32012-12-07 15:50:32 -0500654 param->threshold = NR_THRESHOLD_STATIC;
655 if (BITS_VALUE(rc, 16, 1))
656 param->threshold = NR_THRESHOLD_DYNAMIC;
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400657 param->luma.max_blend_ratio = BITS_VALUE(rc, 24, 4);
658 param->luma.scale_diff_ratio = BITS_VALUE(rc, 12, 4);
659 param->luma.diff_limit_ratio = BITS_VALUE(rc, 8, 4);
660 param->luma.scale_motion_ratio = BITS_VALUE(rc, 4, 4);
661 param->luma.blend_limit_ratio = BITS_VALUE(rc, 0, 4);
662
663 rc = readl_relaxed(VCAP_VP_NR_CHROMA_CONFIG);
664 param->chroma.max_blend_ratio = BITS_VALUE(rc, 24, 4);
665 param->chroma.scale_diff_ratio = BITS_VALUE(rc, 12, 4);
666 param->chroma.diff_limit_ratio = BITS_VALUE(rc, 8, 4);
667 param->chroma.scale_motion_ratio = BITS_VALUE(rc, 4, 4);
668 param->chroma.blend_limit_ratio = BITS_VALUE(rc, 0, 4);
669}
670
671void s_default_nr_val(struct nr_param *param)
672{
Terence Hampson6a744e32012-12-07 15:50:32 -0500673 param->threshold = NR_THRESHOLD_STATIC;
Terence Hampson3dff4ef2012-06-13 15:20:59 -0400674 param->window = 10;
675 param->decay_ratio = 0;
676 param->luma.max_blend_ratio = 0;
677 param->luma.scale_diff_ratio = 4;
678 param->luma.diff_limit_ratio = 1;
679 param->luma.scale_motion_ratio = 4;
680 param->luma.blend_limit_ratio = 9;
681 param->chroma.max_blend_ratio = 0;
682 param->chroma.scale_diff_ratio = 4;
683 param->chroma.diff_limit_ratio = 1;
684 param->chroma.scale_motion_ratio = 4;
685 param->chroma.blend_limit_ratio = 9;
686}
687
Terence Hampson54d3a6d2012-07-10 14:05:35 -0400688int vp_dummy_event(struct vcap_client_data *c_data)
689{
690 struct vcap_dev *dev = c_data->dev;
691 unsigned int width, height;
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400692 struct ion_handle *handle = NULL;
Terence Hampson8d1c3272012-07-27 16:36:31 -0400693 unsigned long paddr, len;
Terence Hampson54d3a6d2012-07-10 14:05:35 -0400694 uint32_t reg;
695 int rc = 0;
696
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400697 pr_debug("%s: Start VP dummy event\n", __func__);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400698 handle = ion_alloc(dev->ion_client, 0x1200, SZ_4K,
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700699 ION_HEAP(ION_CP_MM_HEAP_ID), 0);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400700 if (IS_ERR_OR_NULL(handle)) {
701 pr_err("%s: ion_alloc failed\n", __func__);
Terence Hampson54d3a6d2012-07-10 14:05:35 -0400702 return -ENOMEM;
703 }
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400704
Terence Hampson8d1c3272012-07-27 16:36:31 -0400705 rc = ion_map_iommu(dev->ion_client, handle,
706 dev->domain_num, 0, SZ_4K, 0, &paddr, &len,
707 0, 0);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400708 if (rc < 0) {
Terence Hampson8d1c3272012-07-27 16:36:31 -0400709 pr_err("%s: map_iommu failed\n", __func__);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400710 ion_free(dev->ion_client, handle);
711 return rc;
712 }
Terence Hampson54d3a6d2012-07-10 14:05:35 -0400713
714 width = c_data->vp_out_fmt.width;
715 height = c_data->vp_out_fmt.height;
716
717 c_data->vp_out_fmt.width = 0x3F;
718 c_data->vp_out_fmt.height = 0x16;
719
720 config_vp_format(c_data);
721 writel_relaxed(paddr, VCAP_VP_T1_Y_BASE_ADDR);
722 writel_relaxed(paddr + 0x2C0, VCAP_VP_T1_C_BASE_ADDR);
723 writel_relaxed(paddr + 0x440, VCAP_VP_T2_Y_BASE_ADDR);
724 writel_relaxed(paddr + 0x700, VCAP_VP_T2_C_BASE_ADDR);
725 writel_relaxed(paddr + 0x880, VCAP_VP_OUT_Y_BASE_ADDR);
726 writel_relaxed(paddr + 0xB40, VCAP_VP_OUT_C_BASE_ADDR);
727 writel_iowmb(paddr + 0x1100, VCAP_VP_MOTION_EST_ADDR);
728 writel_relaxed(4 << 20 | 0x2 << 4, VCAP_VP_IN_CONFIG);
729 writel_relaxed(4 << 20 | 0x1 << 4, VCAP_VP_OUT_CONFIG);
730
731 dev->vp_dummy_event = true;
732
Terence Hampsonb128b982012-08-16 14:41:19 -0400733 enable_irq(dev->vpirq->start);
Terence Hampson54d3a6d2012-07-10 14:05:35 -0400734 writel_relaxed(0x01100101, VCAP_VP_INTERRUPT_ENABLE);
735 writel_iowmb(0x00000000, VCAP_VP_CTRL);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400736 writel_iowmb(0x00010000, VCAP_VP_CTRL);
Terence Hampson54d3a6d2012-07-10 14:05:35 -0400737
Terence Hampson54d3a6d2012-07-10 14:05:35 -0400738 rc = wait_event_interruptible_timeout(dev->vp_dummy_waitq,
739 dev->vp_dummy_complete, msecs_to_jiffies(50));
740 if (!rc && !dev->vp_dummy_complete) {
741 pr_err("%s: VP dummy event timeout", __func__);
742 rc = -ETIME;
Terence Hampson54d3a6d2012-07-10 14:05:35 -0400743
Terence Hampsonb128b982012-08-16 14:41:19 -0400744 vp_sw_reset(dev);
Terence Hampson54d3a6d2012-07-10 14:05:35 -0400745 dev->vp_dummy_complete = false;
746 }
747
748 writel_relaxed(0x00000000, VCAP_VP_INTERRUPT_ENABLE);
749 disable_irq(dev->vpirq->start);
750 dev->vp_dummy_event = false;
751
752 reg = readl_relaxed(VCAP_OFFSET(0x0D94));
753 writel_relaxed(reg, VCAP_OFFSET(0x0D9C));
754
755 c_data->vp_out_fmt.width = width;
756 c_data->vp_out_fmt.height = height;
Terence Hampson8d1c3272012-07-27 16:36:31 -0400757 ion_unmap_iommu(dev->ion_client, handle, dev->domain_num, 0);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400758 ion_free(dev->ion_client, handle);
Terence Hampson54d3a6d2012-07-10 14:05:35 -0400759
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400760 pr_debug("%s: Exit VP dummy event\n", __func__);
Terence Hampson54d3a6d2012-07-10 14:05:35 -0400761 return rc;
762}
763
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400764int kickoff_vp(struct vcap_client_data *c_data)
765{
766 struct vcap_dev *dev;
767 struct vp_action *vp_act;
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400768 struct timeval tv;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400769 unsigned long flags = 0;
770 unsigned int chroma_fmt = 0;
771 int size;
Terence Hampson9c4f8a22012-09-12 10:25:50 -0400772 bool top_field = 0;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400773
774 if (!c_data->streaming)
775 return -ENOEXEC;
776
777 dev = c_data->dev;
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400778 pr_debug("Start VP Kickoff\n");
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400779
780 if (dev->vp_client == NULL) {
781 pr_err("No active vp client\n");
782 return -ENODEV;
783 }
Terence Hampson2cba63f2012-08-21 10:54:38 -0400784 vp_act = &dev->vp_client->vp_action;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400785
786 spin_lock_irqsave(&dev->vp_client->cap_slock, flags);
787 if (list_empty(&vp_act->in_active)) {
788 spin_unlock_irqrestore(&dev->vp_client->cap_slock, flags);
789 pr_err("%s: VP We have no more input buffers\n",
790 __func__);
791 return -EAGAIN;
792 }
793
794 vp_act->bufT1 = list_entry(vp_act->in_active.next,
795 struct vcap_buffer, list);
796 list_del(&vp_act->bufT1->list);
797
798 if (list_empty(&vp_act->in_active)) {
799 spin_unlock_irqrestore(&dev->vp_client->cap_slock, flags);
800 list_add(&vp_act->bufT1->list, &vp_act->in_active);
801 pr_err("%s: VP We have no more input buffers\n",
802 __func__);
803 return -EAGAIN;
804 }
805
806 vp_act->bufT2 = list_entry(vp_act->in_active.next,
807 struct vcap_buffer, list);
808 list_del(&vp_act->bufT2->list);
809
810 if (list_empty(&vp_act->out_active)) {
811 spin_unlock_irqrestore(&dev->vp_client->cap_slock, flags);
812 list_add(&vp_act->bufT2->list, &vp_act->in_active);
813 list_add(&vp_act->bufT1->list, &vp_act->in_active);
814 pr_err("%s: VP We have no more output buffers\n",
815 __func__);
816 return -EAGAIN;
817 }
818
819 vp_act->bufOut = list_entry(vp_act->out_active.next,
820 struct vcap_buffer, list);
821 list_del(&vp_act->bufOut->list);
822 spin_unlock_irqrestore(&dev->vp_client->cap_slock, flags);
823
824 size = c_data->vp_in_fmt.height * c_data->vp_in_fmt.width;
825 writel_relaxed(vp_act->bufT1->paddr, VCAP_VP_T1_Y_BASE_ADDR);
826 writel_relaxed(vp_act->bufT1->paddr + size, VCAP_VP_T1_C_BASE_ADDR);
827
828 config_in_buffer(c_data, vp_act->bufT2);
829 config_out_buffer(c_data, vp_act->bufOut);
830
831 /* Config VP */
832 if (c_data->vp_in_fmt.pixfmt == V4L2_PIX_FMT_NV16)
833 chroma_fmt = 1;
834 writel_relaxed((c_data->vp_in_fmt.width / 16) << 20 |
835 chroma_fmt << 11 | 0x2 << 4, VCAP_VP_IN_CONFIG);
836
837 chroma_fmt = 0;
Terence Hampson779dc762012-06-07 15:59:27 -0400838 if (c_data->vp_out_fmt.pixfmt == V4L2_PIX_FMT_NV16)
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400839 chroma_fmt = 1;
840
Terence Hampsonad33c512012-07-16 17:50:00 -0400841 writel_relaxed((c_data->vp_out_fmt.width / 16) << 20 |
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400842 chroma_fmt << 11 | 0x1 << 4, VCAP_VP_OUT_CONFIG);
843
844 /* Enable Interrupt */
Terence Hampsonaa64d3a2012-10-29 13:05:37 -0400845 if (vp_act->bufT2->vb.v4l2_buf.field == V4L2_FIELD_BOTTOM)
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400846 top_field = 1;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400847 vp_act->vp_state = VP_FRAME2;
Terence Hampson72452632012-08-13 12:32:24 -0400848 writel_relaxed(0x01100001, VCAP_VP_INTERRUPT_ENABLE);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400849 writel_iowmb(0x00000000 | top_field, VCAP_VP_CTRL);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400850 writel_iowmb(0x00010000 | top_field, VCAP_VP_CTRL);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400851 atomic_set(&c_data->dev->vp_enabled, 1);
852 enable_irq(dev->vpirq->start);
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400853
854 do_gettimeofday(&tv);
855 dev->dbg_p.vp_timestamp = (uint32_t) (tv.tv_sec * VCAP_USEC +
856 tv.tv_usec);
857
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400858 return 0;
859}
860
861int continue_vp(struct vcap_client_data *c_data)
862{
863 struct vcap_dev *dev;
864 struct vp_action *vp_act;
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400865 struct timeval tv;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400866 int rc;
Terence Hampson9c4f8a22012-09-12 10:25:50 -0400867 bool top_field = 0;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400868
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400869 pr_debug("Start VP Continue\n");
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400870 dev = c_data->dev;
871
872 if (dev->vp_client == NULL) {
873 pr_err("No active vp client\n");
874 return -ENODEV;
875 }
Terence Hampson2cba63f2012-08-21 10:54:38 -0400876 vp_act = &dev->vp_client->vp_action;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400877
878 if (vp_act->vp_state == VP_UNKNOWN) {
879 pr_err("%s: VP is in an unknown state\n",
880 __func__);
881 return -EAGAIN;
882 }
883
884 rc = vp_setup_buffers(c_data);
885 if (rc < 0)
886 return rc;
887
Terence Hampsonaa64d3a2012-10-29 13:05:37 -0400888 if (vp_act->bufT2->vb.v4l2_buf.field == V4L2_FIELD_BOTTOM)
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400889 top_field = 1;
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400890
891 /* Config VP & Enable Interrupt */
Terence Hampson72452632012-08-13 12:32:24 -0400892 writel_relaxed(0x01100001, VCAP_VP_INTERRUPT_ENABLE);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400893 writel_iowmb(0x00000000 | top_field, VCAP_VP_CTRL);
Terence Hampsona2cba0d2012-08-08 11:39:53 -0400894 writel_iowmb(0x00010000 | top_field, VCAP_VP_CTRL);
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400895
896 atomic_set(&c_data->dev->vp_enabled, 1);
897 enable_irq(dev->vpirq->start);
Terence Hampsonc4aa4c02012-08-31 04:49:21 -0400898
899 do_gettimeofday(&tv);
900 dev->dbg_p.vp_timestamp = (uint32_t) (tv.tv_sec * VCAP_USEC +
901 tv.tv_usec);
902
Terence Hampsonaeb793e2012-05-11 11:41:16 -0400903 return 0;
904}