rockchip/rga: add rga support yuv 10 bit
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / rga2 / rga2_drv.c
1 /*
2  * Copyright (C) 2012 ROCKCHIP, Inc.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14
15 #define pr_fmt(fmt) "rga: " fmt
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/sched.h>
21 #include <linux/mutex.h>
22 #include <linux/err.h>
23 #include <linux/clk.h>
24 #include <asm/delay.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/delay.h>
27 #include <asm/io.h>
28 #include <linux/irq.h>
29 #include <linux/interrupt.h>
30 #include <linux/fs.h>
31 #include <asm/uaccess.h>
32 #include <linux/miscdevice.h>
33 #include <linux/poll.h>
34 #include <linux/delay.h>
35 #include <linux/wait.h>
36 #include <linux/syscalls.h>
37 #include <linux/timer.h>
38 #include <linux/time.h>
39 #include <asm/cacheflush.h>
40 #include <linux/slab.h>
41 #include <linux/fb.h>
42 #include <linux/wakelock.h>
43 #include <linux/scatterlist.h>
44 #include <linux/rockchip_ion.h>
45 #include <linux/version.h>
46 #include <linux/pm_runtime.h>
47
48 #include "rga2.h"
49 #include "rga2_reg_info.h"
50 #include "rga2_mmu_info.h"
51 #include "RGA2_API.h"
52 #include "rga2_rop.h"
53
54 #if defined(CONFIG_RK_IOMMU) && defined(CONFIG_ION_ROCKCHIP)
55 #define CONFIG_RGA_IOMMU
56 #endif
57
58 #define RGA2_TEST_FLUSH_TIME 0
59 #define RGA2_INFO_BUS_ERROR 1
60 #define RGA2_POWER_OFF_DELAY    4*HZ /* 4s */
61 #define RGA2_TIMEOUT_DELAY      2*HZ /* 2s */
62 #define RGA2_MAJOR              255
63 #define RGA2_RESET_TIMEOUT      1000
64
65 /* Driver information */
66 #define DRIVER_DESC             "RGA2 Device Driver"
67 #define DRIVER_NAME             "rga2"
68 #define RGA2_VERSION   "2.000"
69
70 ktime_t rga2_start;
71 ktime_t rga2_end;
72 int rga2_flag;
73 int first_RGA2_proc;
74
75 rga2_session rga2_session_global;
76 long (*rga_ioctl_kernel_p)(struct rga_req *);
77
78 struct rga2_drvdata_t {
79         struct miscdevice miscdev;
80         struct device *dev;
81         void *rga_base;
82         int irq;
83
84         struct delayed_work power_off_work;
85         struct wake_lock wake_lock;
86         void (*rga_irq_callback)(int rga_retval);
87
88         struct clk *aclk_rga2;
89         struct clk *hclk_rga2;
90         struct clk *rga2;
91
92         struct ion_client * ion_client;
93 };
94
95 struct rga2_drvdata_t *rga2_drvdata;
96 struct rga2_service_info rga2_service;
97 struct rga2_mmu_buf_t rga2_mmu_buf;
98
99 static int rga2_blit_async(rga2_session *session, struct rga2_req *req);
100 static void rga2_del_running_list(void);
101 static void rga2_del_running_list_timeout(void);
102 static void rga2_try_set_reg(void);
103
104
105 /* Logging */
106 #define RGA_DEBUG 0
107 #if RGA_DEBUG
108 #define DBG(format, args...) printk(KERN_DEBUG "%s: " format, DRIVER_NAME, ## args)
109 #define ERR(format, args...) printk(KERN_ERR "%s: " format, DRIVER_NAME, ## args)
110 #define WARNING(format, args...) printk(KERN_WARN "%s: " format, DRIVER_NAME, ## args)
111 #define INFO(format, args...) printk(KERN_INFO "%s: " format, DRIVER_NAME, ## args)
112 #else
113 #define DBG(format, args...)
114 #define ERR(format, args...)
115 #define WARNING(format, args...)
116 #define INFO(format, args...)
117 #endif
118
119 #if RGA2_TEST_MSG
120 static void print_info(struct rga2_req *req)
121 {
122         printk("render_mode=%d bitblt_mode=%d rotate_mode=%.8x\n",
123                 req->render_mode, req->bitblt_mode, req->rotate_mode);
124         printk("src : y=%.lx uv=%.lx v=%.lx format=%d aw=%d ah=%d vw=%d vh=%d xoff=%d yoff=%d \n",
125                 req->src.yrgb_addr, req->src.uv_addr, req->src.v_addr, req->src.format,
126                 req->src.act_w, req->src.act_h, req->src.vir_w, req->src.vir_h,
127                 req->src.x_offset, req->src.y_offset);
128         printk("dst : y=%lx uv=%lx v=%lx format=%d aw=%d ah=%d vw=%d vh=%d xoff=%d yoff=%d \n",
129                 req->dst.yrgb_addr, req->dst.uv_addr, req->dst.v_addr, req->dst.format,
130                 req->dst.act_w, req->dst.act_h, req->dst.vir_w, req->dst.vir_h,
131                 req->dst.x_offset, req->dst.y_offset);
132         printk("mmu : src=%.2x src1=%.2x dst=%.2x els=%.2x\n",
133                 req->mmu_info.src0_mmu_flag, req->mmu_info.src1_mmu_flag,
134                 req->mmu_info.dst_mmu_flag,  req->mmu_info.els_mmu_flag);
135         printk("alpha : flag %.8x mode0=%.8x mode1=%.8x\n",
136                 req->alpha_rop_flag, req->alpha_mode_0, req->alpha_mode_1);
137 }
138 #endif
139
140 static inline void rga2_write(u32 b, u32 r)
141 {
142         *((volatile unsigned int *)(rga2_drvdata->rga_base + r)) = b;
143 }
144
145 static inline u32 rga2_read(u32 r)
146 {
147         return *((volatile unsigned int *)(rga2_drvdata->rga_base + r));
148 }
149
150 static void rga2_soft_reset(void)
151 {
152         u32 i;
153         u32 reg;
154
155         rga2_write((1 << 3) | (1 << 4), RGA2_SYS_CTRL); //RGA_SYS_CTRL
156
157         for(i = 0; i < RGA2_RESET_TIMEOUT; i++)
158         {
159                 reg = rga2_read(RGA2_SYS_CTRL) & 1; //RGA_SYS_CTRL
160
161                 if(reg == 0)
162                         break;
163
164                 udelay(1);
165         }
166
167         if(i == RGA2_RESET_TIMEOUT)
168                 ERR("soft reset timeout.\n");
169 }
170
171 static void rga2_dump(void)
172 {
173         int running;
174         struct rga2_reg *reg, *reg_tmp;
175         rga2_session *session, *session_tmp;
176
177         running = atomic_read(&rga2_service.total_running);
178         printk("rga total_running %d\n", running);
179         list_for_each_entry_safe(session, session_tmp, &rga2_service.session,
180                 list_session)
181         {
182                 printk("session pid %d:\n", session->pid);
183                 running = atomic_read(&session->task_running);
184                 printk("task_running %d\n", running);
185                 list_for_each_entry_safe(reg, reg_tmp, &session->waiting, session_link)
186                 {
187                         printk("waiting register set 0x%.lu\n", (unsigned long)reg);
188                 }
189                 list_for_each_entry_safe(reg, reg_tmp, &session->running, session_link)
190                 {
191                         printk("running register set 0x%.lu\n", (unsigned long)reg);
192                 }
193         }
194 }
195
196 static inline void rga2_queue_power_off_work(void)
197 {
198         queue_delayed_work(system_wq, &rga2_drvdata->power_off_work,
199                 RGA2_POWER_OFF_DELAY);
200 }
201
202 /* Caller must hold rga_service.lock */
203 static void rga2_power_on(void)
204 {
205         static ktime_t last;
206         ktime_t now = ktime_get();
207
208 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
209         pm_runtime_get_sync(rga2_drvdata->dev);
210 #endif
211
212         if (ktime_to_ns(ktime_sub(now, last)) > NSEC_PER_SEC) {
213                 cancel_delayed_work_sync(&rga2_drvdata->power_off_work);
214                 rga2_queue_power_off_work();
215                 last = now;
216         }
217
218         if (rga2_service.enable)
219                 return;
220
221         clk_prepare_enable(rga2_drvdata->rga2);
222         clk_prepare_enable(rga2_drvdata->aclk_rga2);
223         clk_prepare_enable(rga2_drvdata->hclk_rga2);
224         wake_lock(&rga2_drvdata->wake_lock);
225         rga2_service.enable = true;
226 }
227
228 /* Caller must hold rga_service.lock */
229 static void rga2_power_off(void)
230 {
231         int total_running;
232
233         if (!rga2_service.enable) {
234                 return;
235         }
236
237         total_running = atomic_read(&rga2_service.total_running);
238         if (total_running) {
239                 pr_err("power off when %d task running!!\n", total_running);
240                 mdelay(50);
241                 pr_err("delay 50 ms for running task\n");
242                 rga2_dump();
243         }
244
245         clk_disable_unprepare(rga2_drvdata->rga2);
246         clk_disable_unprepare(rga2_drvdata->aclk_rga2);
247         clk_disable_unprepare(rga2_drvdata->hclk_rga2);
248
249 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
250         pm_runtime_put(rga2_drvdata->dev);
251 #endif
252
253         wake_unlock(&rga2_drvdata->wake_lock);
254     first_RGA2_proc = 0;
255         rga2_service.enable = false;
256 }
257
258 static void rga2_power_off_work(struct work_struct *work)
259 {
260         if (mutex_trylock(&rga2_service.lock)) {
261                 rga2_power_off();
262                 mutex_unlock(&rga2_service.lock);
263         } else {
264                 /* Come back later if the device is busy... */
265                 rga2_queue_power_off_work();
266         }
267 }
268
269 static int rga2_flush(rga2_session *session, unsigned long arg)
270 {
271     int ret = 0;
272     int ret_timeout;
273
274     #if RGA2_TEST_FLUSH_TIME
275     ktime_t start;
276     ktime_t end;
277     start = ktime_get();
278     #endif
279
280     ret_timeout = wait_event_timeout(session->wait, atomic_read(&session->done), RGA2_TIMEOUT_DELAY);
281
282         if (unlikely(ret_timeout < 0)) {
283                 //pr_err("flush pid %d wait task ret %d\n", session->pid, ret);
284         mutex_lock(&rga2_service.lock);
285         rga2_del_running_list();
286         mutex_unlock(&rga2_service.lock);
287         ret = ret_timeout;
288         } else if (0 == ret_timeout) {
289                 //pr_err("flush pid %d wait %d task done timeout\n", session->pid, atomic_read(&session->task_running));
290         //printk("bus  = %.8x\n", rga_read(RGA_INT));
291         mutex_lock(&rga2_service.lock);
292         rga2_del_running_list_timeout();
293         rga2_try_set_reg();
294         mutex_unlock(&rga2_service.lock);
295                 ret = -ETIMEDOUT;
296         }
297
298     #if RGA2_TEST_FLUSH_TIME
299     end = ktime_get();
300     end = ktime_sub(end, start);
301     printk("one flush wait time %d\n", (int)ktime_to_us(end));
302     #endif
303
304         return ret;
305 }
306
307
308 static int rga2_get_result(rga2_session *session, unsigned long arg)
309 {
310         int ret = 0;
311         int num_done;
312
313         num_done = atomic_read(&session->num_done);
314         if (unlikely(copy_to_user((void __user *)arg, &num_done, sizeof(int)))) {
315             printk("copy_to_user failed\n");
316             ret =  -EFAULT;
317         }
318         return ret;
319 }
320
321
322 static int rga2_check_param(const struct rga2_req *req)
323 {
324         if(!((req->render_mode == color_fill_mode)))
325         {
326             if (unlikely((req->src.act_w <= 0) || (req->src.act_w > 8191) || (req->src.act_h <= 0) || (req->src.act_h > 8191)))
327             {
328                 printk("invalid source resolution act_w = %d, act_h = %d\n", req->src.act_w, req->src.act_h);
329                 return -EINVAL;
330             }
331         }
332
333         if(!((req->render_mode == color_fill_mode)))
334         {
335             if (unlikely((req->src.vir_w <= 0) || (req->src.vir_w > 8191) || (req->src.vir_h <= 0) || (req->src.vir_h > 8191)))
336             {
337                 printk("invalid source resolution vir_w = %d, vir_h = %d\n", req->src.vir_w, req->src.vir_h);
338                 return -EINVAL;
339             }
340         }
341
342         //check dst width and height
343         if (unlikely((req->dst.act_w <= 0) || (req->dst.act_w > 4096) || (req->dst.act_h <= 0) || (req->dst.act_h > 4096)))
344         {
345             printk("invalid destination resolution act_w = %d, act_h = %d\n", req->dst.act_w, req->dst.act_h);
346             return -EINVAL;
347         }
348
349         if (unlikely((req->dst.vir_w <= 0) || (req->dst.vir_w > 4096) || (req->dst.vir_h <= 0) || (req->dst.vir_h > 4096)))
350         {
351             printk("invalid destination resolution vir_w = %d, vir_h = %d\n", req->dst.vir_w, req->dst.vir_h);
352             return -EINVAL;
353         }
354
355         //check src_vir_w
356         if(unlikely(req->src.vir_w < req->src.act_w)){
357             printk("invalid src_vir_w act_w = %d, vir_w = %d\n", req->src.act_w, req->src.vir_w);
358             return -EINVAL;
359         }
360
361         //check dst_vir_w
362         if(unlikely(req->dst.vir_w < req->dst.act_w)){
363             if(req->rotate_mode != 1)
364             {
365                 printk("invalid dst_vir_w act_h = %d, vir_h = %d\n", req->dst.act_w, req->dst.vir_w);
366                 return -EINVAL;
367             }
368         }
369
370         return 0;
371 }
372
373 static void rga2_copy_reg(struct rga2_reg *reg, uint32_t offset)
374 {
375     uint32_t i;
376     uint32_t *cmd_buf;
377     uint32_t *reg_p;
378
379     if(atomic_read(&reg->session->task_running) != 0)
380         printk(KERN_ERR "task_running is no zero\n");
381
382     atomic_add(1, &rga2_service.cmd_num);
383         atomic_add(1, &reg->session->task_running);
384
385     cmd_buf = (uint32_t *)rga2_service.cmd_buff + offset*32;
386     reg_p = (uint32_t *)reg->cmd_reg;
387
388     for(i=0; i<32; i++)
389         cmd_buf[i] = reg_p[i];
390 }
391
392
393 static struct rga2_reg * rga2_reg_init(rga2_session *session, struct rga2_req *req)
394 {
395     int32_t ret;
396         struct rga2_reg *reg = kzalloc(sizeof(struct rga2_reg), GFP_KERNEL);
397         if (NULL == reg) {
398                 pr_err("kmalloc fail in rga_reg_init\n");
399                 return NULL;
400         }
401
402     reg->session = session;
403         INIT_LIST_HEAD(&reg->session_link);
404         INIT_LIST_HEAD(&reg->status_link);
405
406     reg->MMU_base = NULL;
407
408     if ((req->mmu_info.src0_mmu_flag & 1) || (req->mmu_info.src1_mmu_flag & 1)
409         || (req->mmu_info.dst_mmu_flag & 1) || (req->mmu_info.els_mmu_flag & 1))
410     {
411         ret = rga2_set_mmu_info(reg, req);
412         if(ret < 0) {
413             printk("%s, [%d] set mmu info error \n", __FUNCTION__, __LINE__);
414             if(reg != NULL)
415                 kfree(reg);
416
417             return NULL;
418         }
419     }
420
421     if(RGA2_gen_reg_info((uint8_t *)reg->cmd_reg, req) == -1) {
422         printk("gen reg info error\n");
423         if(reg != NULL)
424             kfree(reg);
425
426         return NULL;
427     }
428
429     mutex_lock(&rga2_service.lock);
430         list_add_tail(&reg->status_link, &rga2_service.waiting);
431         list_add_tail(&reg->session_link, &session->waiting);
432         mutex_unlock(&rga2_service.lock);
433
434     return reg;
435 }
436
437
438 /* Caller must hold rga_service.lock */
439 static void rga2_reg_deinit(struct rga2_reg *reg)
440 {
441         list_del_init(&reg->session_link);
442         list_del_init(&reg->status_link);
443         kfree(reg);
444 }
445
446 /* Caller must hold rga_service.lock */
447 static void rga2_reg_from_wait_to_run(struct rga2_reg *reg)
448 {
449         list_del_init(&reg->status_link);
450         list_add_tail(&reg->status_link, &rga2_service.running);
451
452         list_del_init(&reg->session_link);
453         list_add_tail(&reg->session_link, &reg->session->running);
454 }
455
456 /* Caller must hold rga_service.lock */
457 static void rga2_service_session_clear(rga2_session *session)
458 {
459         struct rga2_reg *reg, *n;
460
461         list_for_each_entry_safe(reg, n, &session->waiting, session_link)
462         {
463                 rga2_reg_deinit(reg);
464         }
465
466         list_for_each_entry_safe(reg, n, &session->running, session_link)
467         {
468                 rga2_reg_deinit(reg);
469         }
470 }
471
472 /* Caller must hold rga_service.lock */
473 static void rga2_try_set_reg(void)
474 {
475         struct rga2_reg *reg ;
476
477         if (list_empty(&rga2_service.running))
478         {
479                 if (!list_empty(&rga2_service.waiting))
480                 {
481                         /* RGA is idle */
482                         reg = list_entry(rga2_service.waiting.next, struct rga2_reg, status_link);
483
484                         rga2_power_on();
485                         udelay(1);
486
487                         rga2_copy_reg(reg, 0);
488                         rga2_reg_from_wait_to_run(reg);
489
490 #ifdef CONFIG_ARM
491                         dmac_flush_range(&rga2_service.cmd_buff[0], &rga2_service.cmd_buff[32]);
492                         outer_flush_range(virt_to_phys(&rga2_service.cmd_buff[0]),virt_to_phys(&rga2_service.cmd_buff[32]));
493 #elif defined(CONFIG_ARM64)
494                         __dma_flush_range(&rga2_service.cmd_buff[0], &rga2_service.cmd_buff[32]);
495 #endif
496
497                         //rga2_soft_reset();
498
499                         rga2_write(0x0, RGA2_SYS_CTRL);
500
501                         /* CMD buff */
502                         rga2_write(virt_to_phys(rga2_service.cmd_buff), RGA2_CMD_BASE);
503
504 #if RGA2_TEST
505                         if(rga2_flag) {
506                                 int32_t i, *p;
507                                 p = rga2_service.cmd_buff;
508                                 printk("CMD_REG\n");
509                                 for (i=0; i<8; i++)
510                                         printk("%.8x %.8x %.8x %.8x\n", p[0 + i*4], p[1+i*4], p[2 + i*4], p[3 + i*4]);
511                         }
512 #endif
513
514                         /* master mode */
515                         rga2_write((0x1<<1)|(0x1<<2)|(0x1<<5)|(0x1<<6), RGA2_SYS_CTRL);
516
517                         /* All CMD finish int */
518                         rga2_write(rga2_read(RGA2_INT)|(0x1<<10)|(0x1<<9)|(0x1<<8), RGA2_INT);
519
520 #if RGA2_TEST_TIME
521                         rga2_start = ktime_get();
522 #endif
523
524                         /* Start proc */
525                         atomic_set(&reg->session->done, 0);
526                         rga2_write(0x1, RGA2_CMD_CTRL);
527 #if RGA2_TEST
528                         if(rga2_flag)
529                         {
530                                 uint32_t i;
531                                 printk("CMD_READ_BACK_REG\n");
532                                 for (i=0; i<8; i++)
533                                         printk("%.8x %.8x %.8x %.8x\n", rga2_read(0x100 + i*16 + 0),
534                                                         rga2_read(0x100 + i*16 + 4), rga2_read(0x100 + i*16 + 8), rga2_read(0x100 + i*16 + 12));
535                         }
536 #endif
537                 }
538         }
539 }
540
541 /* Caller must hold rga_service.lock */
542 static void rga2_del_running_list(void)
543 {
544         struct rga2_reg *reg;
545
546         while(!list_empty(&rga2_service.running))
547         {
548                 reg = list_entry(rga2_service.running.next, struct rga2_reg, status_link);
549
550                 if(reg->MMU_len != 0)
551                 {
552                         if (rga2_mmu_buf.back + reg->MMU_len > 2*rga2_mmu_buf.size)
553                                 rga2_mmu_buf.back = reg->MMU_len + rga2_mmu_buf.size;
554                         else
555                                 rga2_mmu_buf.back += reg->MMU_len;
556                 }
557                 atomic_sub(1, &reg->session->task_running);
558                 atomic_sub(1, &rga2_service.total_running);
559
560                 if(list_empty(&reg->session->waiting))
561                 {
562                         atomic_set(&reg->session->done, 1);
563                         wake_up(&reg->session->wait);
564                 }
565
566                 rga2_reg_deinit(reg);
567         }
568 }
569
570 /* Caller must hold rga_service.lock */
571 static void rga2_del_running_list_timeout(void)
572 {
573     struct rga2_reg *reg;
574
575     while(!list_empty(&rga2_service.running))
576     {
577         reg = list_entry(rga2_service.running.next, struct rga2_reg, status_link);
578
579         if(reg->MMU_base != NULL)
580         {
581             kfree(reg->MMU_base);
582         }
583
584         atomic_sub(1, &reg->session->task_running);
585         atomic_sub(1, &rga2_service.total_running);
586
587         rga2_soft_reset();
588
589         if(list_empty(&reg->session->waiting))
590         {
591             atomic_set(&reg->session->done, 1);
592             wake_up(&reg->session->wait);
593         }
594
595         rga2_reg_deinit(reg);
596     }
597 }
598
599 static int rga2_convert_dma_buf(struct rga2_req *req)
600 {
601         struct ion_handle *hdl;
602         ion_phys_addr_t phy_addr;
603         size_t len;
604         int ret;
605         uint32_t src_vir_w, dst_vir_w;
606
607         src_vir_w = req->src.vir_w;
608         dst_vir_w = req->dst.vir_w;
609
610         req->sg_src0 = NULL;
611         req->sg_src1 = NULL;
612         req->sg_dst  = NULL;
613         req->sg_els  = NULL;
614
615         if ((int)req->src.yrgb_addr > 0) {
616                 hdl = ion_import_dma_buf(rga2_drvdata->ion_client,
617                                          req->src.yrgb_addr);
618                 if (IS_ERR(hdl)) {
619                         ret = PTR_ERR(hdl);
620                         printk("RGA2 SRC ERROR ion buf handle\n");
621                         return ret;
622                 }
623                 if (req->mmu_info.src0_mmu_flag) {
624                         req->sg_src0 = ion_sg_table(rga2_drvdata->ion_client,
625                                                     hdl);
626                         req->src.yrgb_addr = req->src.uv_addr;
627                         req->src.uv_addr = req->src.yrgb_addr
628                                         + (src_vir_w * req->src.vir_h);
629                         req->src.v_addr = req->src.uv_addr
630                                         + (src_vir_w * req->src.vir_h) / 4;
631                 } else {
632                         ion_phys(rga2_drvdata->ion_client, hdl, &phy_addr,
633                                  &len);
634                         req->src.yrgb_addr = phy_addr;
635                         req->src.uv_addr = req->src.yrgb_addr
636                                         + (src_vir_w * req->src.vir_h);
637                         req->src.v_addr = req->src.uv_addr
638                                         + (src_vir_w * req->src.vir_h) / 4;
639                 }
640                 ion_free(rga2_drvdata->ion_client, hdl);
641         } else {
642                 req->src.yrgb_addr = req->src.uv_addr;
643                 req->src.uv_addr = req->src.yrgb_addr
644                                         + (src_vir_w * req->src.vir_h);
645                 req->src.v_addr = req->src.uv_addr
646                                         + (src_vir_w * req->src.vir_h) / 4;
647         }
648
649         if ((int)req->dst.yrgb_addr > 0) {
650                 hdl = ion_import_dma_buf(rga2_drvdata->ion_client,
651                                          req->dst.yrgb_addr);
652                 if (IS_ERR(hdl)) {
653                         ret = PTR_ERR(hdl);
654                         printk("RGA2 DST ERROR ion buf handle\n");
655                         return ret;
656                 }
657                 if (req->mmu_info.dst_mmu_flag) {
658                         req->sg_dst = ion_sg_table(rga2_drvdata->ion_client,
659                                                    hdl);
660                         req->dst.yrgb_addr = req->dst.uv_addr;
661                         req->dst.uv_addr = req->dst.yrgb_addr
662                                         + (dst_vir_w * req->dst.vir_h);
663                         req->dst.v_addr = req->dst.uv_addr
664                                         + (dst_vir_w * req->dst.vir_h) / 4;
665                 } else {
666                         ion_phys(rga2_drvdata->ion_client, hdl, &phy_addr,
667                                  &len);
668                         req->dst.yrgb_addr = phy_addr;
669                         req->dst.uv_addr = req->dst.yrgb_addr
670                                         + (dst_vir_w * req->dst.vir_h);
671                         req->dst.v_addr = req->dst.uv_addr
672                                         + (dst_vir_w * req->dst.vir_h) / 4;
673                 }
674                 ion_free(rga2_drvdata->ion_client, hdl);
675         } else {
676                 req->dst.yrgb_addr = req->dst.uv_addr;
677                 req->dst.uv_addr = req->dst.yrgb_addr
678                                         + (dst_vir_w * req->dst.vir_h);
679                 req->dst.v_addr = req->dst.uv_addr
680                                         + (dst_vir_w * req->dst.vir_h) / 4;
681         }
682
683         if ((int)req->src1.yrgb_addr > 0) {
684                 hdl = ion_import_dma_buf(rga2_drvdata->ion_client,
685                                          req->src1.yrgb_addr);
686                 if (IS_ERR(hdl)) {
687                         ret = PTR_ERR(hdl);
688                         printk("RGA2 ERROR ion buf handle\n");
689                         return ret;
690                 }
691                 if (req->mmu_info.dst_mmu_flag) {
692                         req->sg_src1 = ion_sg_table(rga2_drvdata->ion_client,
693                                                     hdl);
694                         req->src1.yrgb_addr = req->src1.uv_addr;
695                         req->src1.uv_addr = req->src1.yrgb_addr
696                                         + (req->src1.vir_w * req->src1.vir_h);
697                         req->src1.v_addr = req->src1.uv_addr
698                                 + (req->src1.vir_w * req->src1.vir_h) / 4;
699                 } else {
700                         ion_phys(rga2_drvdata->ion_client, hdl, &phy_addr,
701                                  &len);
702                         req->src1.yrgb_addr = phy_addr;
703                         req->src1.uv_addr = req->src1.yrgb_addr
704                                         + (req->src1.vir_w * req->src1.vir_h);
705                         req->src1.v_addr = req->src1.uv_addr
706                                 + (req->src1.vir_w * req->src1.vir_h) / 4;
707                 }
708                 ion_free(rga2_drvdata->ion_client, hdl);
709         } else {
710                 req->src1.yrgb_addr = req->src1.uv_addr;
711                 req->src1.uv_addr = req->src1.yrgb_addr
712                                         + (req->src1.vir_w * req->src1.vir_h);
713                 req->src1.v_addr = req->src1.uv_addr
714                                 + (req->src1.vir_w * req->src1.vir_h) / 4;
715         }
716
717         return 0;
718 }
719
720 static int rga2_blit(rga2_session *session, struct rga2_req *req)
721 {
722         int ret = -1;
723         int num = 0;
724         struct rga2_reg *reg;
725
726         if(rga2_convert_dma_buf(req)) {
727                 printk("RGA2 : DMA buf copy error\n");
728                 return -EFAULT;
729         }
730
731         do {
732                 /* check value if legal */
733                 ret = rga2_check_param(req);
734                 if(ret == -EINVAL) {
735                         printk("req argument is inval\n");
736                         break;
737                 }
738
739                 reg = rga2_reg_init(session, req);
740                 if(reg == NULL) {
741                         break;
742                 }
743                 num = 1;
744
745                 mutex_lock(&rga2_service.lock);
746                 atomic_add(num, &rga2_service.total_running);
747                 rga2_try_set_reg();
748                 mutex_unlock(&rga2_service.lock);
749
750                 return 0;
751         }
752         while(0);
753
754         return -EFAULT;
755 }
756
757 static int rga2_blit_async(rga2_session *session, struct rga2_req *req)
758 {
759         int ret = -1;
760
761 #if RGA2_TEST_MSG
762         if (1) {//req->src.format >= 0x10) {
763                 print_info(req);
764                 rga2_flag = 1;
765                 printk("*** rga_blit_async proc ***\n");
766         }
767         else
768                 rga2_flag = 0;
769 #endif
770         atomic_set(&session->done, 0);
771         ret = rga2_blit(session, req);
772
773         return ret;
774         }
775
776 static int rga2_blit_sync(rga2_session *session, struct rga2_req *req)
777 {
778         int ret = -1;
779         int ret_timeout = 0;
780
781 #if RGA2_TEST_MSG
782         if (1) {//req->bitblt_mode == 0x2) {
783                 print_info(req);
784                 rga2_flag = 1;
785                 printk("*** rga2_blit_sync proc ***\n");
786         }
787         else
788                 rga2_flag = 0;
789 #endif
790
791         atomic_set(&session->done, 0);
792
793         ret = rga2_blit(session, req);
794         if(ret < 0)
795                 return ret;
796
797         ret_timeout = wait_event_timeout(session->wait, atomic_read(&session->done), RGA2_TIMEOUT_DELAY);
798
799         if (unlikely(ret_timeout< 0))
800         {
801                 //pr_err("sync pid %d wait task ret %d\n", session->pid, ret_timeout);
802                 mutex_lock(&rga2_service.lock);
803                 rga2_del_running_list();
804                 mutex_unlock(&rga2_service.lock);
805                 ret = ret_timeout;
806         }
807         else if (0 == ret_timeout)
808         {
809                 //pr_err("sync pid %d wait %d task done timeout\n", session->pid, atomic_read(&session->task_running));
810                 mutex_lock(&rga2_service.lock);
811                 rga2_del_running_list_timeout();
812                 rga2_try_set_reg();
813                 mutex_unlock(&rga2_service.lock);
814                 ret = -ETIMEDOUT;
815         }
816
817 #if RGA2_TEST_TIME
818         rga2_end = ktime_get();
819         rga2_end = ktime_sub(rga2_end, rga2_start);
820         printk("sync one cmd end time %d\n", (int)ktime_to_us(rga2_end));
821 #endif
822
823         return ret;
824         }
825
826 static long rga_ioctl(struct file *file, uint32_t cmd, unsigned long arg)
827 {
828         struct rga2_req req, req_first;
829         struct rga_req req_rga;
830         int ret = 0;
831         rga2_session *session;
832
833         memset(&req, 0x0, sizeof(req));
834
835         mutex_lock(&rga2_service.mutex);
836
837         session = (rga2_session *)file->private_data;
838
839         if (NULL == session)
840         {
841                 printk("%s [%d] rga thread session is null\n",__FUNCTION__,__LINE__);
842                 mutex_unlock(&rga2_service.mutex);
843                 return -EINVAL;
844         }
845
846         memset(&req, 0x0, sizeof(req));
847
848         switch (cmd)
849         {
850                 case RGA_BLIT_SYNC:
851                         if (unlikely(copy_from_user(&req_rga, (struct rga_req*)arg, sizeof(struct rga_req))))
852                         {
853                                 ERR("copy_from_user failed\n");
854                                 ret = -EFAULT;
855                                 break;
856                         }
857                         RGA_MSG_2_RGA2_MSG(&req_rga, &req);
858
859                         if (first_RGA2_proc == 0 && req.bitblt_mode == bitblt_mode && rga2_service.dev_mode == 1) {
860                                 memcpy(&req_first, &req, sizeof(struct rga2_req));
861                                 if ((req_first.src.act_w != req_first.dst.act_w)
862                                                 || (req_first.src.act_h != req_first.dst.act_h)) {
863                                         req_first.src.act_w = MIN(320, MIN(req_first.src.act_w, req_first.dst.act_w));
864                                         req_first.src.act_h = MIN(240, MIN(req_first.src.act_h, req_first.dst.act_h));
865                                         req_first.dst.act_w = req_first.src.act_w;
866                                         req_first.dst.act_h = req_first.src.act_h;
867                                         ret = rga2_blit_async(session, &req_first);
868                                 }
869                                 ret = rga2_blit_sync(session, &req);
870                                 first_RGA2_proc = 1;
871                         }
872                         else {
873                                 ret = rga2_blit_sync(session, &req);
874                         }
875                         break;
876                 case RGA_BLIT_ASYNC:
877                         if (unlikely(copy_from_user(&req_rga, (struct rga_req*)arg, sizeof(struct rga_req))))
878                         {
879                                 ERR("copy_from_user failed\n");
880                                 ret = -EFAULT;
881                                 break;
882                         }
883
884                         RGA_MSG_2_RGA2_MSG(&req_rga, &req);
885
886                         if (first_RGA2_proc == 0 && req.bitblt_mode == bitblt_mode && rga2_service.dev_mode == 1) {
887                                 memcpy(&req_first, &req, sizeof(struct rga2_req));
888                                 if ((req_first.src.act_w != req_first.dst.act_w)
889                                                 || (req_first.src.act_h != req_first.dst.act_h)) {
890                                         req_first.src.act_w = MIN(320, MIN(req_first.src.act_w, req_first.dst.act_w));
891                                         req_first.src.act_h = MIN(240, MIN(req_first.src.act_h, req_first.dst.act_h));
892                                         req_first.dst.act_w = req_first.src.act_w;
893                                         req_first.dst.act_h = req_first.src.act_h;
894                                         ret = rga2_blit_async(session, &req_first);
895                                 }
896                                 ret = rga2_blit_async(session, &req);
897                                 first_RGA2_proc = 1;
898                         }
899                         else {
900                                 ret = rga2_blit_async(session, &req);
901                         }
902                         break;
903                 case RGA2_BLIT_SYNC:
904                         if (unlikely(copy_from_user(&req, (struct rga2_req*)arg, sizeof(struct rga2_req))))
905                         {
906                                 ERR("copy_from_user failed\n");
907                                 ret = -EFAULT;
908                                 break;
909                         }
910                         ret = rga2_blit_sync(session, &req);
911                         break;
912                 case RGA2_BLIT_ASYNC:
913                         if (unlikely(copy_from_user(&req, (struct rga2_req*)arg, sizeof(struct rga2_req))))
914                         {
915                                 ERR("copy_from_user failed\n");
916                                 ret = -EFAULT;
917                                 break;
918                         }
919
920                         if((atomic_read(&rga2_service.total_running) > 16))
921                         {
922                                 ret = rga2_blit_sync(session, &req);
923                         }
924                         else
925                         {
926                                 ret = rga2_blit_async(session, &req);
927                         }
928                         break;
929                 case RGA_FLUSH:
930                 case RGA2_FLUSH:
931                         ret = rga2_flush(session, arg);
932                         break;
933                 case RGA_GET_RESULT:
934                 case RGA2_GET_RESULT:
935                         ret = rga2_get_result(session, arg);
936                         break;
937                 case RGA_GET_VERSION:
938                 case RGA2_GET_VERSION:
939                         ret = copy_to_user((void *)arg, RGA2_VERSION, sizeof(RGA2_VERSION));
940                         //ret = 0;
941                         break;
942                 default:
943                         ERR("unknown ioctl cmd!\n");
944                         ret = -EINVAL;
945                         break;
946         }
947
948         mutex_unlock(&rga2_service.mutex);
949
950         return ret;
951 }
952
953 #ifdef CONFIG_COMPAT
954 static long compat_rga_ioctl(struct file *file, uint32_t cmd, unsigned long arg)
955 {
956         struct rga2_req req, req_first;
957         struct rga_req_32 req_rga;
958         int ret = 0;
959         rga2_session *session;
960
961         memset(&req, 0x0, sizeof(req));
962
963         mutex_lock(&rga2_service.mutex);
964
965         session = (rga2_session *)file->private_data;
966
967 #if RGA2_TEST_MSG
968         printk("use compat_rga_ioctl\n");
969 #endif
970
971         if (NULL == session) {
972                 printk("%s [%d] rga thread session is null\n",__FUNCTION__,__LINE__);
973                 mutex_unlock(&rga2_service.mutex);
974                 return -EINVAL;
975         }
976
977         memset(&req, 0x0, sizeof(req));
978
979         switch (cmd) {
980                 case RGA_BLIT_SYNC:
981                         if (unlikely(copy_from_user(&req_rga, compat_ptr((compat_uptr_t)arg), sizeof(struct rga_req_32))))
982                         {
983                                 ERR("copy_from_user failed\n");
984                                 ret = -EFAULT;
985                                 break;
986                         }
987
988                         RGA_MSG_2_RGA2_MSG_32(&req_rga, &req);
989
990                         if (first_RGA2_proc == 0 && req.bitblt_mode == bitblt_mode && rga2_service.dev_mode == 1) {
991                                 memcpy(&req_first, &req, sizeof(struct rga2_req));
992                                 if ((req_first.src.act_w != req_first.dst.act_w)
993                                                 || (req_first.src.act_h != req_first.dst.act_h)) {
994                                         req_first.src.act_w = MIN(320, MIN(req_first.src.act_w, req_first.dst.act_w));
995                                         req_first.src.act_h = MIN(240, MIN(req_first.src.act_h, req_first.dst.act_h));
996                                         req_first.dst.act_w = req_first.src.act_w;
997                                         req_first.dst.act_h = req_first.src.act_h;
998                                         ret = rga2_blit_async(session, &req_first);
999                                 }
1000                                 ret = rga2_blit_sync(session, &req);
1001                                 first_RGA2_proc = 1;
1002                         }
1003                         else {
1004                                 ret = rga2_blit_sync(session, &req);
1005                         }
1006                         break;
1007                 case RGA_BLIT_ASYNC:
1008                         if (unlikely(copy_from_user(&req_rga, compat_ptr((compat_uptr_t)arg), sizeof(struct rga_req_32))))
1009                         {
1010                                 ERR("copy_from_user failed\n");
1011                                 ret = -EFAULT;
1012                                 break;
1013                         }
1014                         RGA_MSG_2_RGA2_MSG_32(&req_rga, &req);
1015
1016                         if (first_RGA2_proc == 0 && req.bitblt_mode == bitblt_mode && rga2_service.dev_mode == 1) {
1017                                 memcpy(&req_first, &req, sizeof(struct rga2_req));
1018                                 if ((req_first.src.act_w != req_first.dst.act_w)
1019                                                 || (req_first.src.act_h != req_first.dst.act_h)) {
1020                                         req_first.src.act_w = MIN(320, MIN(req_first.src.act_w, req_first.dst.act_w));
1021                                         req_first.src.act_h = MIN(240, MIN(req_first.src.act_h, req_first.dst.act_h));
1022                                         req_first.dst.act_w = req_first.src.act_w;
1023                                         req_first.dst.act_h = req_first.src.act_h;
1024                                         ret = rga2_blit_async(session, &req_first);
1025                                 }
1026                                 ret = rga2_blit_sync(session, &req);
1027                                 first_RGA2_proc = 1;
1028                         }
1029                         else {
1030                                 ret = rga2_blit_sync(session, &req);
1031                         }
1032
1033                         //if((atomic_read(&rga2_service.total_running) > 8))
1034                         //    ret = rga2_blit_sync(session, &req);
1035                         //else
1036                         //    ret = rga2_blit_async(session, &req);
1037
1038                         break;
1039                 case RGA2_BLIT_SYNC:
1040                         if (unlikely(copy_from_user(&req, compat_ptr((compat_uptr_t)arg), sizeof(struct rga2_req))))
1041                         {
1042                                 ERR("copy_from_user failed\n");
1043                                 ret = -EFAULT;
1044                                 break;
1045                         }
1046                         ret = rga2_blit_sync(session, &req);
1047                         break;
1048                 case RGA2_BLIT_ASYNC:
1049                         if (unlikely(copy_from_user(&req, compat_ptr((compat_uptr_t)arg), sizeof(struct rga2_req))))
1050                         {
1051                                 ERR("copy_from_user failed\n");
1052                                 ret = -EFAULT;
1053                                 break;
1054                         }
1055
1056                         if((atomic_read(&rga2_service.total_running) > 16))
1057                                 ret = rga2_blit_sync(session, &req);
1058                         else
1059                                 ret = rga2_blit_async(session, &req);
1060
1061                         break;
1062                 case RGA_FLUSH:
1063                 case RGA2_FLUSH:
1064                         ret = rga2_flush(session, arg);
1065                         break;
1066                 case RGA_GET_RESULT:
1067                 case RGA2_GET_RESULT:
1068                         ret = rga2_get_result(session, arg);
1069                         break;
1070                 case RGA_GET_VERSION:
1071                 case RGA2_GET_VERSION:
1072                         ret = copy_to_user((void *)arg, RGA2_VERSION, sizeof(RGA2_VERSION));
1073                         //ret = 0;
1074                         break;
1075                 default:
1076                         ERR("unknown ioctl cmd!\n");
1077                         ret = -EINVAL;
1078                         break;
1079         }
1080
1081         mutex_unlock(&rga2_service.mutex);
1082
1083         return ret;
1084 }
1085 #endif
1086
1087
1088 long rga2_ioctl_kernel(struct rga_req *req_rga)
1089 {
1090         int ret = 0;
1091         rga2_session *session;
1092         struct rga2_req req;
1093
1094         memset(&req, 0x0, sizeof(req));
1095         mutex_lock(&rga2_service.mutex);
1096         session = &rga2_session_global;
1097         if (NULL == session)
1098         {
1099                 printk("%s [%d] rga thread session is null\n",__FUNCTION__,__LINE__);
1100                 mutex_unlock(&rga2_service.mutex);
1101                 return -EINVAL;
1102         }
1103
1104         RGA_MSG_2_RGA2_MSG(req_rga, &req);
1105         ret = rga2_blit_sync(session, &req);
1106         mutex_unlock(&rga2_service.mutex);
1107
1108         return ret;
1109 }
1110
1111
1112 static int rga2_open(struct inode *inode, struct file *file)
1113 {
1114         rga2_session *session = kzalloc(sizeof(rga2_session), GFP_KERNEL);
1115
1116         if (NULL == session) {
1117                 pr_err("unable to allocate memory for rga_session.");
1118                 return -ENOMEM;
1119         }
1120
1121         session->pid = current->pid;
1122         INIT_LIST_HEAD(&session->waiting);
1123         INIT_LIST_HEAD(&session->running);
1124         INIT_LIST_HEAD(&session->list_session);
1125         init_waitqueue_head(&session->wait);
1126         mutex_lock(&rga2_service.lock);
1127         list_add_tail(&session->list_session, &rga2_service.session);
1128         mutex_unlock(&rga2_service.lock);
1129         atomic_set(&session->task_running, 0);
1130         atomic_set(&session->num_done, 0);
1131         file->private_data = (void *)session;
1132
1133         return nonseekable_open(inode, file);
1134 }
1135
1136 static int rga2_release(struct inode *inode, struct file *file)
1137 {
1138         int task_running;
1139         rga2_session *session = (rga2_session *)file->private_data;
1140
1141         if (NULL == session)
1142                 return -EINVAL;
1143
1144         task_running = atomic_read(&session->task_running);
1145         if (task_running)
1146         {
1147                 pr_err("rga2_service session %d still has %d task running when closing\n", session->pid, task_running);
1148                 msleep(100);
1149         }
1150
1151         wake_up(&session->wait);
1152         mutex_lock(&rga2_service.lock);
1153         list_del(&session->list_session);
1154         rga2_service_session_clear(session);
1155         kfree(session);
1156         mutex_unlock(&rga2_service.lock);
1157
1158         return 0;
1159 }
1160
1161 static irqreturn_t rga2_irq_thread(int irq, void *dev_id)
1162 {
1163         mutex_lock(&rga2_service.lock);
1164         if (rga2_service.enable) {
1165                 rga2_del_running_list();
1166                 rga2_try_set_reg();
1167         }
1168         mutex_unlock(&rga2_service.lock);
1169
1170         return IRQ_HANDLED;
1171 }
1172
1173 static irqreturn_t rga2_irq(int irq,  void *dev_id)
1174 {
1175         /*clear INT */
1176         rga2_write(rga2_read(RGA2_INT) | (0x1<<4) | (0x1<<5) | (0x1<<6) | (0x1<<7), RGA2_INT);
1177
1178         return IRQ_WAKE_THREAD;
1179 }
1180
1181 struct file_operations rga2_fops = {
1182         .owner          = THIS_MODULE,
1183         .open           = rga2_open,
1184         .release        = rga2_release,
1185         .unlocked_ioctl         = rga_ioctl,
1186 #ifdef CONFIG_COMPAT
1187         .compat_ioctl           = compat_rga_ioctl,
1188 #endif
1189 };
1190
1191 static struct miscdevice rga2_dev ={
1192         .minor = RGA2_MAJOR,
1193         .name  = "rga",
1194         .fops  = &rga2_fops,
1195 };
1196
1197 static const struct of_device_id rockchip_rga_dt_ids[] = {
1198         { .compatible = "rockchip,rga2", },
1199         {},
1200 };
1201
1202 static int rga2_drv_probe(struct platform_device *pdev)
1203 {
1204         struct rga2_drvdata_t *data;
1205         struct resource *res;
1206         int ret = 0;
1207         struct device_node *np = pdev->dev.of_node;
1208
1209         mutex_init(&rga2_service.lock);
1210         mutex_init(&rga2_service.mutex);
1211         atomic_set(&rga2_service.total_running, 0);
1212         atomic_set(&rga2_service.src_format_swt, 0);
1213         rga2_service.last_prc_src_format = 1; /* default is yuv first*/
1214         rga2_service.enable = false;
1215
1216         rga_ioctl_kernel_p = rga2_ioctl_kernel;
1217
1218         data = devm_kzalloc(&pdev->dev, sizeof(struct rga2_drvdata_t), GFP_KERNEL);
1219         if(NULL == data)
1220         {
1221                 ERR("failed to allocate driver data.\n");
1222                 return -ENOMEM;
1223         }
1224
1225         INIT_DELAYED_WORK(&data->power_off_work, rga2_power_off_work);
1226         wake_lock_init(&data->wake_lock, WAKE_LOCK_SUSPEND, "rga");
1227
1228         data->rga2 = devm_clk_get(&pdev->dev, "clk_rga");
1229         data->aclk_rga2 = devm_clk_get(&pdev->dev, "aclk_rga");
1230         data->hclk_rga2 = devm_clk_get(&pdev->dev, "hclk_rga");
1231
1232         /* map the registers */
1233         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1234         data->rga_base = devm_ioremap_resource(&pdev->dev, res);
1235         if (!data->rga_base) {
1236                 ERR("rga ioremap failed\n");
1237                 ret = -ENOENT;
1238                 goto err_ioremap;
1239         }
1240
1241         /* get the IRQ */
1242         data->irq = platform_get_irq(pdev, 0);
1243         if (data->irq <= 0) {
1244                 ERR("failed to get rga irq resource (%d).\n", data->irq);
1245                 ret = data->irq;
1246                 goto err_irq;
1247         }
1248
1249         /* request the IRQ */
1250         ret = devm_request_threaded_irq(&pdev->dev, data->irq, rga2_irq, rga2_irq_thread, 0, "rga", pdev);
1251         if (ret)
1252         {
1253                 ERR("rga request_irq failed (%d).\n", ret);
1254                 goto err_irq;
1255         }
1256
1257         platform_set_drvdata(pdev, data);
1258         data->dev = &pdev->dev;
1259         rga2_drvdata = data;
1260         of_property_read_u32(np, "dev_mode", &rga2_service.dev_mode);
1261
1262 #if defined(CONFIG_ION_ROCKCHIP)
1263         data->ion_client = rockchip_ion_client_create("rga");
1264         if (IS_ERR(data->ion_client)) {
1265                 dev_err(&pdev->dev, "failed to create ion client for rga");
1266                 return PTR_ERR(data->ion_client);
1267         } else {
1268                 dev_info(&pdev->dev, "rga ion client create success!\n");
1269         }
1270 #endif
1271
1272         ret = misc_register(&rga2_dev);
1273         if(ret)
1274         {
1275                 ERR("cannot register miscdev (%d)\n", ret);
1276                 goto err_misc_register;
1277         }
1278
1279 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
1280         pm_runtime_enable(&pdev->dev);
1281 #endif
1282
1283         pr_info("Driver loaded succesfully\n");
1284
1285         return 0;
1286
1287 err_misc_register:
1288         free_irq(data->irq, pdev);
1289 err_irq:
1290         iounmap(data->rga_base);
1291 err_ioremap:
1292         wake_lock_destroy(&data->wake_lock);
1293         //kfree(data);
1294
1295         return ret;
1296 }
1297
1298 static int rga2_drv_remove(struct platform_device *pdev)
1299 {
1300         struct rga2_drvdata_t *data = platform_get_drvdata(pdev);
1301         DBG("%s [%d]\n",__FUNCTION__,__LINE__);
1302
1303         wake_lock_destroy(&data->wake_lock);
1304         misc_deregister(&(data->miscdev));
1305         free_irq(data->irq, &data->miscdev);
1306         iounmap((void __iomem *)(data->rga_base));
1307
1308         devm_clk_put(&pdev->dev, data->rga2);
1309         devm_clk_put(&pdev->dev, data->aclk_rga2);
1310         devm_clk_put(&pdev->dev, data->hclk_rga2);
1311
1312 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
1313         pm_runtime_disable(&pdev->dev);
1314 #endif
1315
1316         kfree(data);
1317         return 0;
1318 }
1319
1320 static struct platform_driver rga2_driver = {
1321         .probe          = rga2_drv_probe,
1322         .remove         = rga2_drv_remove,
1323         .driver         = {
1324                 .owner  = THIS_MODULE,
1325                 .name   = "rga2",
1326                 .of_match_table = of_match_ptr(rockchip_rga_dt_ids),
1327         },
1328 };
1329
1330
1331 void rga2_test_0(void);
1332
1333 static int __init rga2_init(void)
1334 {
1335         int ret;
1336         uint32_t *buf_p;
1337
1338         /* malloc pre scale mid buf mmu table */
1339         buf_p = kmalloc(1024*256, GFP_KERNEL);
1340         rga2_mmu_buf.buf_virtual = buf_p;
1341         rga2_mmu_buf.buf = (uint32_t *)virt_to_phys((void *)((unsigned long)buf_p));
1342         rga2_mmu_buf.front = 0;
1343         rga2_mmu_buf.back = 64*1024;
1344         rga2_mmu_buf.size = 64*1024;
1345
1346         rga2_mmu_buf.pages = kmalloc(32768 * sizeof(struct page *), GFP_KERNEL);
1347
1348         ret = platform_driver_register(&rga2_driver);
1349         if (ret != 0) {
1350                 printk(KERN_ERR "Platform device register failed (%d).\n", ret);
1351                 return ret;
1352         }
1353
1354         rga2_session_global.pid = 0x0000ffff;
1355         INIT_LIST_HEAD(&rga2_session_global.waiting);
1356         INIT_LIST_HEAD(&rga2_session_global.running);
1357         INIT_LIST_HEAD(&rga2_session_global.list_session);
1358
1359         INIT_LIST_HEAD(&rga2_service.waiting);
1360         INIT_LIST_HEAD(&rga2_service.running);
1361         INIT_LIST_HEAD(&rga2_service.done);
1362         INIT_LIST_HEAD(&rga2_service.session);
1363         init_waitqueue_head(&rga2_session_global.wait);
1364         //mutex_lock(&rga_service.lock);
1365         list_add_tail(&rga2_session_global.list_session, &rga2_service.session);
1366         //mutex_unlock(&rga_service.lock);
1367         atomic_set(&rga2_session_global.task_running, 0);
1368         atomic_set(&rga2_session_global.num_done, 0);
1369
1370 #if RGA2_TEST_CASE
1371         rga2_test_0();
1372 #endif
1373
1374         INFO("Module initialized.\n");
1375
1376         return 0;
1377 }
1378
1379 static void __exit rga2_exit(void)
1380 {
1381         rga2_power_off();
1382
1383         if (rga2_mmu_buf.buf_virtual)
1384                 kfree(rga2_mmu_buf.buf_virtual);
1385
1386         platform_driver_unregister(&rga2_driver);
1387 }
1388
1389
1390 #if RGA2_TEST_CASE
1391
1392 void rga2_test_0(void)
1393 {
1394         struct rga2_req req;
1395         rga2_session session;
1396         unsigned int *src, *dst;
1397
1398         session.pid     = current->pid;
1399         INIT_LIST_HEAD(&session.waiting);
1400         INIT_LIST_HEAD(&session.running);
1401         INIT_LIST_HEAD(&session.list_session);
1402         init_waitqueue_head(&session.wait);
1403         /* no need to protect */
1404         list_add_tail(&session.list_session, &rga2_service.session);
1405         atomic_set(&session.task_running, 0);
1406         atomic_set(&session.num_done, 0);
1407
1408         memset(&req, 0, sizeof(struct rga2_req));
1409         src = kmalloc(800*480*4, GFP_KERNEL);
1410         dst = kmalloc(800*480*4, GFP_KERNEL);
1411
1412         printk("\n********************************\n");
1413         printk("************ RGA2_TEST ************\n");
1414         printk("********************************\n\n");
1415
1416 #if 1
1417         memset(src, 0x80, 800 * 480 * 4);
1418         memset(dst, 0xcc, 800 * 480 * 4);
1419 #endif
1420 #if 0
1421         dmac_flush_range(src, &src[800 * 480]);
1422         outer_flush_range(virt_to_phys(src), virt_to_phys(&src[800 * 480]));
1423
1424         dmac_flush_range(dst, &dst[800 * 480]);
1425         outer_flush_range(virt_to_phys(dst), virt_to_phys(&dst[800 * 480]));
1426 #endif
1427
1428 #if 0
1429         req.pat.act_w = 16;
1430         req.pat.act_h = 16;
1431         req.pat.vir_w = 16;
1432         req.pat.vir_h = 16;
1433         req.pat.yrgb_addr = virt_to_phys(src);
1434         req.render_mode = 0;
1435         rga2_blit_sync(&session, &req);
1436 #endif
1437         {
1438                 uint32_t i, j;
1439                 uint8_t *sp;
1440
1441                 sp = (uint8_t *)src;
1442                 for (j = 0; j < 240; j++) {
1443                         sp = (uint8_t *)src + j * 320 * 10 / 8;
1444                         for (i = 0; i < 320; i++) {
1445                                 if ((i & 3) == 0) {
1446                                         sp[i * 5 / 4] = 0;
1447                                         sp[i * 5 / 4+1] = 0x1;
1448                                 } else if ((i & 3) == 1) {
1449                                         sp[i * 5 / 4+1] = 0x4;
1450                                 } else if ((i & 3) == 2) {
1451                                         sp[i * 5 / 4+1] = 0x10;
1452                                 } else if ((i & 3) == 3) {
1453                                         sp[i * 5 / 4+1] = 0x40;
1454                             }
1455                         }
1456                 }
1457                 sp = (uint8_t *)src;
1458                 for (j = 0; j < 100; j++)
1459                         printk("src %.2x\n", sp[j]);
1460         }
1461         req.src.act_w = 320;
1462         req.src.act_h = 240;
1463
1464         req.src.vir_w = 320;
1465         req.src.vir_h = 240;
1466         req.src.yrgb_addr = 0;//(uint32_t)virt_to_phys(src);
1467         req.src.uv_addr = (unsigned long)virt_to_phys(src);
1468         req.src.v_addr = 0;
1469         req.src.format = RGA2_FORMAT_YCbCr_420_SP_10B;
1470
1471         req.dst.act_w  = 320;
1472         req.dst.act_h = 240;
1473         req.dst.x_offset = 0;
1474         req.dst.y_offset = 0;
1475
1476         req.dst.vir_w = 320;
1477         req.dst.vir_h = 240;
1478
1479         req.dst.yrgb_addr = 0;//((uint32_t)virt_to_phys(dst));
1480         req.dst.uv_addr = (unsigned long)virt_to_phys(dst);
1481         req.dst.format = RGA2_FORMAT_YCbCr_420_SP;
1482
1483         //dst = dst0;
1484
1485         //req.render_mode = color_fill_mode;
1486         //req.fg_color = 0x80ffffff;
1487
1488         req.rotate_mode = 0;
1489         req.scale_bicu_mode = 2;
1490
1491 #if 0
1492         //req.alpha_rop_flag = 0;
1493         //req.alpha_rop_mode = 0x19;
1494         //req.PD_mode = 3;
1495
1496         //req.mmu_info.mmu_flag = 0x21;
1497         //req.mmu_info.mmu_en = 1;
1498
1499         //printk("src = %.8x\n", req.src.yrgb_addr);
1500         //printk("src = %.8x\n", req.src.uv_addr);
1501         //printk("dst = %.8x\n", req.dst.yrgb_addr);
1502 #endif
1503
1504         rga2_blit_sync(&session, &req);
1505
1506 #if 0
1507         uint32_t j;
1508         for (j = 0; j < 320 * 240 * 10 / 8; j++) {
1509         if (src[j] != dst[j])
1510                 printk("error value dst not equal src j %d, s %.2x d %.2x\n",
1511                         j, src[j], dst[j]);
1512         }
1513 #endif
1514
1515 #if 1
1516         {
1517                 uint32_t j;
1518                 uint8_t *dp = (uint8_t *)dst;
1519
1520                 for (j = 0; j < 100; j++)
1521                         printk("%d %.2x\n", j, dp[j]);
1522         }
1523 #endif
1524
1525         if(src)
1526                 kfree(src);
1527         if(dst)
1528                 kfree(dst);
1529 }
1530 #endif
1531
1532 module_init(rga2_init);
1533 module_exit(rga2_exit);
1534
1535 /* Module information */
1536 MODULE_AUTHOR("zsq@rock-chips.com");
1537 MODULE_DESCRIPTION("Driver for rga device");
1538 MODULE_LICENSE("GPL");