Merge branch 'lsk-v4.4-eas-v5.2' of git://git.linaro.org/arm/eas/kernel.git
[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
606         req->sg_src0 = NULL;
607         req->sg_src1 = NULL;
608         req->sg_dst  = NULL;
609         req->sg_els  = NULL;
610
611         if((int)req->src.yrgb_addr > 0) {
612                 hdl = ion_import_dma_buf(rga2_drvdata->ion_client, req->src.yrgb_addr);
613                 if (IS_ERR(hdl)) {
614                         ret = PTR_ERR(hdl);
615                         printk("RGA2 SRC ERROR ion buf handle\n");
616                         return ret;
617                 }
618                 if (req->mmu_info.src0_mmu_flag) {
619                         req->sg_src0 = ion_sg_table(rga2_drvdata->ion_client, hdl);
620                         req->src.yrgb_addr = req->src.uv_addr;
621                         req->src.uv_addr = req->src.yrgb_addr + (req->src.vir_w * req->src.vir_h);
622                         req->src.v_addr = req->src.uv_addr + (req->src.vir_w * req->src.vir_h)/4;
623                 }
624                 else {
625                         ion_phys(rga2_drvdata->ion_client, hdl, &phy_addr, &len);
626                         req->src.yrgb_addr = phy_addr;
627                         req->src.uv_addr = req->src.yrgb_addr + (req->src.vir_w * req->src.vir_h);
628                         req->src.v_addr = req->src.uv_addr + (req->src.vir_w * req->src.vir_h)/4;
629                 }
630                 ion_free(rga2_drvdata->ion_client, hdl);
631         }
632         else {
633                 req->src.yrgb_addr = req->src.uv_addr;
634                 req->src.uv_addr = req->src.yrgb_addr + (req->src.vir_w * req->src.vir_h);
635                 req->src.v_addr = req->src.uv_addr + (req->src.vir_w * req->src.vir_h)/4;
636         }
637
638         if((int)req->dst.yrgb_addr > 0) {
639                 hdl = ion_import_dma_buf(rga2_drvdata->ion_client, req->dst.yrgb_addr);
640                 if (IS_ERR(hdl)) {
641                         ret = PTR_ERR(hdl);
642                         printk("RGA2 DST ERROR ion buf handle\n");
643                         return ret;
644                 }
645                 if (req->mmu_info.dst_mmu_flag) {
646                         req->sg_dst = ion_sg_table(rga2_drvdata->ion_client, hdl);
647                         req->dst.yrgb_addr = req->dst.uv_addr;
648                         req->dst.uv_addr = req->dst.yrgb_addr + (req->dst.vir_w * req->dst.vir_h);
649                         req->dst.v_addr = req->dst.uv_addr + (req->dst.vir_w * req->dst.vir_h)/4;
650                 }
651                 else {
652                         ion_phys(rga2_drvdata->ion_client, hdl, &phy_addr, &len);
653                         req->dst.yrgb_addr = phy_addr;
654                         req->dst.uv_addr = req->dst.yrgb_addr + (req->dst.vir_w * req->dst.vir_h);
655                         req->dst.v_addr = req->dst.uv_addr + (req->dst.vir_w * req->dst.vir_h)/4;
656                 }
657                 ion_free(rga2_drvdata->ion_client, hdl);
658         }
659         else {
660                 req->dst.yrgb_addr = req->dst.uv_addr;
661                 req->dst.uv_addr = req->dst.yrgb_addr + (req->dst.vir_w * req->dst.vir_h);
662                 req->dst.v_addr = req->dst.uv_addr + (req->dst.vir_w * req->dst.vir_h)/4;
663         }
664
665         if((int)req->src1.yrgb_addr > 0) {
666                 hdl = ion_import_dma_buf(rga2_drvdata->ion_client, req->src1.yrgb_addr);
667                 if (IS_ERR(hdl)) {
668                         ret = PTR_ERR(hdl);
669                         printk("RGA2 ERROR ion buf handle\n");
670                         return ret;
671                 }
672                 if (req->mmu_info.dst_mmu_flag) {
673                         req->sg_src1 = ion_sg_table(rga2_drvdata->ion_client, hdl);
674                         req->src1.yrgb_addr = 0;
675                         req->src1.uv_addr = req->dst.yrgb_addr + (req->dst.vir_w * req->dst.vir_h);
676                         req->src1.v_addr = req->dst.uv_addr + (req->dst.vir_w * req->dst.vir_h)/4;
677                 }
678                 else {
679                         ion_phys(rga2_drvdata->ion_client, hdl, &phy_addr, &len);
680                         req->src1.yrgb_addr = phy_addr;
681                         req->src1.uv_addr = req->dst.yrgb_addr + (req->dst.vir_w * req->dst.vir_h);
682                         req->src1.v_addr = req->dst.uv_addr + (req->dst.vir_w * req->dst.vir_h)/4;
683                 }
684                 ion_free(rga2_drvdata->ion_client, hdl);
685         }
686         else {
687                 req->src1.yrgb_addr = req->dst.uv_addr;
688                 req->src1.uv_addr = req->dst.yrgb_addr + (req->dst.vir_w * req->dst.vir_h);
689                 req->src1.v_addr = req->dst.uv_addr + (req->dst.vir_w * req->dst.vir_h)/4;
690         }
691
692         return 0;
693 }
694
695 static int rga2_blit(rga2_session *session, struct rga2_req *req)
696 {
697         int ret = -1;
698         int num = 0;
699         struct rga2_reg *reg;
700
701         if(rga2_convert_dma_buf(req)) {
702                 printk("RGA2 : DMA buf copy error\n");
703                 return -EFAULT;
704         }
705
706         do {
707                 /* check value if legal */
708                 ret = rga2_check_param(req);
709                 if(ret == -EINVAL) {
710                         printk("req argument is inval\n");
711                         break;
712                 }
713
714                 reg = rga2_reg_init(session, req);
715                 if(reg == NULL) {
716                         break;
717                 }
718                 num = 1;
719
720                 mutex_lock(&rga2_service.lock);
721                 atomic_add(num, &rga2_service.total_running);
722                 rga2_try_set_reg();
723                 mutex_unlock(&rga2_service.lock);
724
725                 return 0;
726         }
727         while(0);
728
729         return -EFAULT;
730 }
731
732 static int rga2_blit_async(rga2_session *session, struct rga2_req *req)
733 {
734         int ret = -1;
735
736 #if RGA2_TEST_MSG
737         if (1) {//req->src.format >= 0x10) {
738                 print_info(req);
739                 rga2_flag = 1;
740                 printk("*** rga_blit_async proc ***\n");
741         }
742         else
743                 rga2_flag = 0;
744 #endif
745         atomic_set(&session->done, 0);
746         ret = rga2_blit(session, req);
747
748         return ret;
749         }
750
751 static int rga2_blit_sync(rga2_session *session, struct rga2_req *req)
752 {
753         int ret = -1;
754         int ret_timeout = 0;
755
756 #if RGA2_TEST_MSG
757         if (1) {//req->bitblt_mode == 0x2) {
758                 print_info(req);
759                 rga2_flag = 1;
760                 printk("*** rga2_blit_sync proc ***\n");
761         }
762         else
763                 rga2_flag = 0;
764 #endif
765
766         atomic_set(&session->done, 0);
767
768         ret = rga2_blit(session, req);
769         if(ret < 0)
770                 return ret;
771
772         ret_timeout = wait_event_timeout(session->wait, atomic_read(&session->done), RGA2_TIMEOUT_DELAY);
773
774         if (unlikely(ret_timeout< 0))
775         {
776                 //pr_err("sync pid %d wait task ret %d\n", session->pid, ret_timeout);
777                 mutex_lock(&rga2_service.lock);
778                 rga2_del_running_list();
779                 mutex_unlock(&rga2_service.lock);
780                 ret = ret_timeout;
781         }
782         else if (0 == ret_timeout)
783         {
784                 //pr_err("sync pid %d wait %d task done timeout\n", session->pid, atomic_read(&session->task_running));
785                 mutex_lock(&rga2_service.lock);
786                 rga2_del_running_list_timeout();
787                 rga2_try_set_reg();
788                 mutex_unlock(&rga2_service.lock);
789                 ret = -ETIMEDOUT;
790         }
791
792 #if RGA2_TEST_TIME
793         rga2_end = ktime_get();
794         rga2_end = ktime_sub(rga2_end, rga2_start);
795         printk("sync one cmd end time %d\n", (int)ktime_to_us(rga2_end));
796 #endif
797
798         return ret;
799         }
800
801 static long rga_ioctl(struct file *file, uint32_t cmd, unsigned long arg)
802 {
803         struct rga2_req req, req_first;
804         struct rga_req req_rga;
805         int ret = 0;
806         rga2_session *session;
807
808         memset(&req, 0x0, sizeof(req));
809
810         mutex_lock(&rga2_service.mutex);
811
812         session = (rga2_session *)file->private_data;
813
814         if (NULL == session)
815         {
816                 printk("%s [%d] rga thread session is null\n",__FUNCTION__,__LINE__);
817                 mutex_unlock(&rga2_service.mutex);
818                 return -EINVAL;
819         }
820
821         memset(&req, 0x0, sizeof(req));
822
823         switch (cmd)
824         {
825                 case RGA_BLIT_SYNC:
826                         if (unlikely(copy_from_user(&req_rga, (struct rga_req*)arg, sizeof(struct rga_req))))
827                         {
828                                 ERR("copy_from_user failed\n");
829                                 ret = -EFAULT;
830                                 break;
831                         }
832                         RGA_MSG_2_RGA2_MSG(&req_rga, &req);
833
834                         if (first_RGA2_proc == 0 && req.bitblt_mode == bitblt_mode && rga2_service.dev_mode == 1) {
835                                 memcpy(&req_first, &req, sizeof(struct rga2_req));
836                                 if ((req_first.src.act_w != req_first.dst.act_w)
837                                                 || (req_first.src.act_h != req_first.dst.act_h)) {
838                                         req_first.src.act_w = MIN(320, MIN(req_first.src.act_w, req_first.dst.act_w));
839                                         req_first.src.act_h = MIN(240, MIN(req_first.src.act_h, req_first.dst.act_h));
840                                         req_first.dst.act_w = req_first.src.act_w;
841                                         req_first.dst.act_h = req_first.src.act_h;
842                                         ret = rga2_blit_async(session, &req_first);
843                                 }
844                                 ret = rga2_blit_sync(session, &req);
845                                 first_RGA2_proc = 1;
846                         }
847                         else {
848                                 ret = rga2_blit_sync(session, &req);
849                         }
850                         break;
851                 case RGA_BLIT_ASYNC:
852                         if (unlikely(copy_from_user(&req_rga, (struct rga_req*)arg, sizeof(struct rga_req))))
853                         {
854                                 ERR("copy_from_user failed\n");
855                                 ret = -EFAULT;
856                                 break;
857                         }
858
859                         RGA_MSG_2_RGA2_MSG(&req_rga, &req);
860
861                         if (first_RGA2_proc == 0 && req.bitblt_mode == bitblt_mode && rga2_service.dev_mode == 1) {
862                                 memcpy(&req_first, &req, sizeof(struct rga2_req));
863                                 if ((req_first.src.act_w != req_first.dst.act_w)
864                                                 || (req_first.src.act_h != req_first.dst.act_h)) {
865                                         req_first.src.act_w = MIN(320, MIN(req_first.src.act_w, req_first.dst.act_w));
866                                         req_first.src.act_h = MIN(240, MIN(req_first.src.act_h, req_first.dst.act_h));
867                                         req_first.dst.act_w = req_first.src.act_w;
868                                         req_first.dst.act_h = req_first.src.act_h;
869                                         ret = rga2_blit_async(session, &req_first);
870                                 }
871                                 ret = rga2_blit_async(session, &req);
872                                 first_RGA2_proc = 1;
873                         }
874                         else {
875                                 ret = rga2_blit_async(session, &req);
876                         }
877                         break;
878                 case RGA2_BLIT_SYNC:
879                         if (unlikely(copy_from_user(&req, (struct rga2_req*)arg, sizeof(struct rga2_req))))
880                         {
881                                 ERR("copy_from_user failed\n");
882                                 ret = -EFAULT;
883                                 break;
884                         }
885                         ret = rga2_blit_sync(session, &req);
886                         break;
887                 case RGA2_BLIT_ASYNC:
888                         if (unlikely(copy_from_user(&req, (struct rga2_req*)arg, sizeof(struct rga2_req))))
889                         {
890                                 ERR("copy_from_user failed\n");
891                                 ret = -EFAULT;
892                                 break;
893                         }
894
895                         if((atomic_read(&rga2_service.total_running) > 16))
896                         {
897                                 ret = rga2_blit_sync(session, &req);
898                         }
899                         else
900                         {
901                                 ret = rga2_blit_async(session, &req);
902                         }
903                         break;
904                 case RGA_FLUSH:
905                 case RGA2_FLUSH:
906                         ret = rga2_flush(session, arg);
907                         break;
908                 case RGA_GET_RESULT:
909                 case RGA2_GET_RESULT:
910                         ret = rga2_get_result(session, arg);
911                         break;
912                 case RGA_GET_VERSION:
913                 case RGA2_GET_VERSION:
914                         ret = copy_to_user((void *)arg, RGA2_VERSION, sizeof(RGA2_VERSION));
915                         //ret = 0;
916                         break;
917                 default:
918                         ERR("unknown ioctl cmd!\n");
919                         ret = -EINVAL;
920                         break;
921         }
922
923         mutex_unlock(&rga2_service.mutex);
924
925         return ret;
926 }
927
928 #ifdef CONFIG_COMPAT
929 static long compat_rga_ioctl(struct file *file, uint32_t cmd, unsigned long arg)
930 {
931         struct rga2_req req, req_first;
932         struct rga_req_32 req_rga;
933         int ret = 0;
934         rga2_session *session;
935
936         memset(&req, 0x0, sizeof(req));
937
938         mutex_lock(&rga2_service.mutex);
939
940         session = (rga2_session *)file->private_data;
941
942 #if RGA2_TEST_MSG
943         printk("use compat_rga_ioctl\n");
944 #endif
945
946         if (NULL == session) {
947                 printk("%s [%d] rga thread session is null\n",__FUNCTION__,__LINE__);
948                 mutex_unlock(&rga2_service.mutex);
949                 return -EINVAL;
950         }
951
952         memset(&req, 0x0, sizeof(req));
953
954         switch (cmd) {
955                 case RGA_BLIT_SYNC:
956                         if (unlikely(copy_from_user(&req_rga, compat_ptr((compat_uptr_t)arg), sizeof(struct rga_req_32))))
957                         {
958                                 ERR("copy_from_user failed\n");
959                                 ret = -EFAULT;
960                                 break;
961                         }
962
963                         RGA_MSG_2_RGA2_MSG_32(&req_rga, &req);
964
965                         if (first_RGA2_proc == 0 && req.bitblt_mode == bitblt_mode && rga2_service.dev_mode == 1) {
966                                 memcpy(&req_first, &req, sizeof(struct rga2_req));
967                                 if ((req_first.src.act_w != req_first.dst.act_w)
968                                                 || (req_first.src.act_h != req_first.dst.act_h)) {
969                                         req_first.src.act_w = MIN(320, MIN(req_first.src.act_w, req_first.dst.act_w));
970                                         req_first.src.act_h = MIN(240, MIN(req_first.src.act_h, req_first.dst.act_h));
971                                         req_first.dst.act_w = req_first.src.act_w;
972                                         req_first.dst.act_h = req_first.src.act_h;
973                                         ret = rga2_blit_async(session, &req_first);
974                                 }
975                                 ret = rga2_blit_sync(session, &req);
976                                 first_RGA2_proc = 1;
977                         }
978                         else {
979                                 ret = rga2_blit_sync(session, &req);
980                         }
981                         break;
982                 case RGA_BLIT_ASYNC:
983                         if (unlikely(copy_from_user(&req_rga, compat_ptr((compat_uptr_t)arg), sizeof(struct rga_req_32))))
984                         {
985                                 ERR("copy_from_user failed\n");
986                                 ret = -EFAULT;
987                                 break;
988                         }
989                         RGA_MSG_2_RGA2_MSG_32(&req_rga, &req);
990
991                         if (first_RGA2_proc == 0 && req.bitblt_mode == bitblt_mode && rga2_service.dev_mode == 1) {
992                                 memcpy(&req_first, &req, sizeof(struct rga2_req));
993                                 if ((req_first.src.act_w != req_first.dst.act_w)
994                                                 || (req_first.src.act_h != req_first.dst.act_h)) {
995                                         req_first.src.act_w = MIN(320, MIN(req_first.src.act_w, req_first.dst.act_w));
996                                         req_first.src.act_h = MIN(240, MIN(req_first.src.act_h, req_first.dst.act_h));
997                                         req_first.dst.act_w = req_first.src.act_w;
998                                         req_first.dst.act_h = req_first.src.act_h;
999                                         ret = rga2_blit_async(session, &req_first);
1000                                 }
1001                                 ret = rga2_blit_sync(session, &req);
1002                                 first_RGA2_proc = 1;
1003                         }
1004                         else {
1005                                 ret = rga2_blit_sync(session, &req);
1006                         }
1007
1008                         //if((atomic_read(&rga2_service.total_running) > 8))
1009                         //    ret = rga2_blit_sync(session, &req);
1010                         //else
1011                         //    ret = rga2_blit_async(session, &req);
1012
1013                         break;
1014                 case RGA2_BLIT_SYNC:
1015                         if (unlikely(copy_from_user(&req, compat_ptr((compat_uptr_t)arg), sizeof(struct rga2_req))))
1016                         {
1017                                 ERR("copy_from_user failed\n");
1018                                 ret = -EFAULT;
1019                                 break;
1020                         }
1021                         ret = rga2_blit_sync(session, &req);
1022                         break;
1023                 case RGA2_BLIT_ASYNC:
1024                         if (unlikely(copy_from_user(&req, compat_ptr((compat_uptr_t)arg), sizeof(struct rga2_req))))
1025                         {
1026                                 ERR("copy_from_user failed\n");
1027                                 ret = -EFAULT;
1028                                 break;
1029                         }
1030
1031                         if((atomic_read(&rga2_service.total_running) > 16))
1032                                 ret = rga2_blit_sync(session, &req);
1033                         else
1034                                 ret = rga2_blit_async(session, &req);
1035
1036                         break;
1037                 case RGA_FLUSH:
1038                 case RGA2_FLUSH:
1039                         ret = rga2_flush(session, arg);
1040                         break;
1041                 case RGA_GET_RESULT:
1042                 case RGA2_GET_RESULT:
1043                         ret = rga2_get_result(session, arg);
1044                         break;
1045                 case RGA_GET_VERSION:
1046                 case RGA2_GET_VERSION:
1047                         ret = copy_to_user((void *)arg, RGA2_VERSION, sizeof(RGA2_VERSION));
1048                         //ret = 0;
1049                         break;
1050                 default:
1051                         ERR("unknown ioctl cmd!\n");
1052                         ret = -EINVAL;
1053                         break;
1054         }
1055
1056         mutex_unlock(&rga2_service.mutex);
1057
1058         return ret;
1059 }
1060 #endif
1061
1062
1063 long rga2_ioctl_kernel(struct rga_req *req_rga)
1064 {
1065         int ret = 0;
1066         rga2_session *session;
1067         struct rga2_req req;
1068
1069         memset(&req, 0x0, sizeof(req));
1070         mutex_lock(&rga2_service.mutex);
1071         session = &rga2_session_global;
1072         if (NULL == session)
1073         {
1074                 printk("%s [%d] rga thread session is null\n",__FUNCTION__,__LINE__);
1075                 mutex_unlock(&rga2_service.mutex);
1076                 return -EINVAL;
1077         }
1078
1079         RGA_MSG_2_RGA2_MSG(req_rga, &req);
1080         ret = rga2_blit_sync(session, &req);
1081         mutex_unlock(&rga2_service.mutex);
1082
1083         return ret;
1084 }
1085
1086
1087 static int rga2_open(struct inode *inode, struct file *file)
1088 {
1089         rga2_session *session = kzalloc(sizeof(rga2_session), GFP_KERNEL);
1090
1091         if (NULL == session) {
1092                 pr_err("unable to allocate memory for rga_session.");
1093                 return -ENOMEM;
1094         }
1095
1096         session->pid = current->pid;
1097         INIT_LIST_HEAD(&session->waiting);
1098         INIT_LIST_HEAD(&session->running);
1099         INIT_LIST_HEAD(&session->list_session);
1100         init_waitqueue_head(&session->wait);
1101         mutex_lock(&rga2_service.lock);
1102         list_add_tail(&session->list_session, &rga2_service.session);
1103         mutex_unlock(&rga2_service.lock);
1104         atomic_set(&session->task_running, 0);
1105         atomic_set(&session->num_done, 0);
1106         file->private_data = (void *)session;
1107
1108         return nonseekable_open(inode, file);
1109 }
1110
1111 static int rga2_release(struct inode *inode, struct file *file)
1112 {
1113         int task_running;
1114         rga2_session *session = (rga2_session *)file->private_data;
1115
1116         if (NULL == session)
1117                 return -EINVAL;
1118
1119         task_running = atomic_read(&session->task_running);
1120         if (task_running)
1121         {
1122                 pr_err("rga2_service session %d still has %d task running when closing\n", session->pid, task_running);
1123                 msleep(100);
1124         }
1125
1126         wake_up(&session->wait);
1127         mutex_lock(&rga2_service.lock);
1128         list_del(&session->list_session);
1129         rga2_service_session_clear(session);
1130         kfree(session);
1131         mutex_unlock(&rga2_service.lock);
1132
1133         return 0;
1134 }
1135
1136 static irqreturn_t rga2_irq_thread(int irq, void *dev_id)
1137 {
1138         mutex_lock(&rga2_service.lock);
1139         if (rga2_service.enable) {
1140                 rga2_del_running_list();
1141                 rga2_try_set_reg();
1142         }
1143         mutex_unlock(&rga2_service.lock);
1144
1145         return IRQ_HANDLED;
1146 }
1147
1148 static irqreturn_t rga2_irq(int irq,  void *dev_id)
1149 {
1150         /*clear INT */
1151         rga2_write(rga2_read(RGA2_INT) | (0x1<<4) | (0x1<<5) | (0x1<<6) | (0x1<<7), RGA2_INT);
1152
1153         return IRQ_WAKE_THREAD;
1154 }
1155
1156 struct file_operations rga2_fops = {
1157         .owner          = THIS_MODULE,
1158         .open           = rga2_open,
1159         .release        = rga2_release,
1160         .unlocked_ioctl         = rga_ioctl,
1161 #ifdef CONFIG_COMPAT
1162         .compat_ioctl           = compat_rga_ioctl,
1163 #endif
1164 };
1165
1166 static struct miscdevice rga2_dev ={
1167         .minor = RGA2_MAJOR,
1168         .name  = "rga",
1169         .fops  = &rga2_fops,
1170 };
1171
1172 static const struct of_device_id rockchip_rga_dt_ids[] = {
1173         { .compatible = "rockchip,rga2", },
1174         {},
1175 };
1176
1177 static int rga2_drv_probe(struct platform_device *pdev)
1178 {
1179         struct rga2_drvdata_t *data;
1180         struct resource *res;
1181         int ret = 0;
1182         struct device_node *np = pdev->dev.of_node;
1183
1184         mutex_init(&rga2_service.lock);
1185         mutex_init(&rga2_service.mutex);
1186         atomic_set(&rga2_service.total_running, 0);
1187         atomic_set(&rga2_service.src_format_swt, 0);
1188         rga2_service.last_prc_src_format = 1; /* default is yuv first*/
1189         rga2_service.enable = false;
1190
1191         rga_ioctl_kernel_p = rga2_ioctl_kernel;
1192
1193         data = devm_kzalloc(&pdev->dev, sizeof(struct rga2_drvdata_t), GFP_KERNEL);
1194         if(NULL == data)
1195         {
1196                 ERR("failed to allocate driver data.\n");
1197                 return -ENOMEM;
1198         }
1199
1200         INIT_DELAYED_WORK(&data->power_off_work, rga2_power_off_work);
1201         wake_lock_init(&data->wake_lock, WAKE_LOCK_SUSPEND, "rga");
1202
1203         data->rga2 = devm_clk_get(&pdev->dev, "clk_rga");
1204         data->aclk_rga2 = devm_clk_get(&pdev->dev, "aclk_rga");
1205         data->hclk_rga2 = devm_clk_get(&pdev->dev, "hclk_rga");
1206
1207         /* map the registers */
1208         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1209         data->rga_base = devm_ioremap_resource(&pdev->dev, res);
1210         if (!data->rga_base) {
1211                 ERR("rga ioremap failed\n");
1212                 ret = -ENOENT;
1213                 goto err_ioremap;
1214         }
1215
1216         /* get the IRQ */
1217         data->irq = platform_get_irq(pdev, 0);
1218         if (data->irq <= 0) {
1219                 ERR("failed to get rga irq resource (%d).\n", data->irq);
1220                 ret = data->irq;
1221                 goto err_irq;
1222         }
1223
1224         /* request the IRQ */
1225         ret = devm_request_threaded_irq(&pdev->dev, data->irq, rga2_irq, rga2_irq_thread, 0, "rga", pdev);
1226         if (ret)
1227         {
1228                 ERR("rga request_irq failed (%d).\n", ret);
1229                 goto err_irq;
1230         }
1231
1232         platform_set_drvdata(pdev, data);
1233         data->dev = &pdev->dev;
1234         rga2_drvdata = data;
1235         of_property_read_u32(np, "dev_mode", &rga2_service.dev_mode);
1236
1237 #if defined(CONFIG_ION_ROCKCHIP)
1238         data->ion_client = rockchip_ion_client_create("rga");
1239         if (IS_ERR(data->ion_client)) {
1240                 dev_err(&pdev->dev, "failed to create ion client for rga");
1241                 return PTR_ERR(data->ion_client);
1242         } else {
1243                 dev_info(&pdev->dev, "rga ion client create success!\n");
1244         }
1245 #endif
1246
1247         ret = misc_register(&rga2_dev);
1248         if(ret)
1249         {
1250                 ERR("cannot register miscdev (%d)\n", ret);
1251                 goto err_misc_register;
1252         }
1253
1254 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
1255         pm_runtime_enable(&pdev->dev);
1256 #endif
1257
1258         pr_info("Driver loaded succesfully\n");
1259
1260         return 0;
1261
1262 err_misc_register:
1263         free_irq(data->irq, pdev);
1264 err_irq:
1265         iounmap(data->rga_base);
1266 err_ioremap:
1267         wake_lock_destroy(&data->wake_lock);
1268         //kfree(data);
1269
1270         return ret;
1271 }
1272
1273 static int rga2_drv_remove(struct platform_device *pdev)
1274 {
1275         struct rga2_drvdata_t *data = platform_get_drvdata(pdev);
1276         DBG("%s [%d]\n",__FUNCTION__,__LINE__);
1277
1278         wake_lock_destroy(&data->wake_lock);
1279         misc_deregister(&(data->miscdev));
1280         free_irq(data->irq, &data->miscdev);
1281         iounmap((void __iomem *)(data->rga_base));
1282
1283         devm_clk_put(&pdev->dev, data->rga2);
1284         devm_clk_put(&pdev->dev, data->aclk_rga2);
1285         devm_clk_put(&pdev->dev, data->hclk_rga2);
1286
1287 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
1288         pm_runtime_disable(&pdev->dev);
1289 #endif
1290
1291         kfree(data);
1292         return 0;
1293 }
1294
1295 static struct platform_driver rga2_driver = {
1296         .probe          = rga2_drv_probe,
1297         .remove         = rga2_drv_remove,
1298         .driver         = {
1299                 .owner  = THIS_MODULE,
1300                 .name   = "rga2",
1301                 .of_match_table = of_match_ptr(rockchip_rga_dt_ids),
1302         },
1303 };
1304
1305
1306 void rga2_test_0(void);
1307
1308 static int __init rga2_init(void)
1309 {
1310         int ret;
1311         uint32_t *buf_p;
1312
1313         /* malloc pre scale mid buf mmu table */
1314         buf_p = kmalloc(1024*256, GFP_KERNEL);
1315         rga2_mmu_buf.buf_virtual = buf_p;
1316         rga2_mmu_buf.buf = (uint32_t *)virt_to_phys((void *)((unsigned long)buf_p));
1317         rga2_mmu_buf.front = 0;
1318         rga2_mmu_buf.back = 64*1024;
1319         rga2_mmu_buf.size = 64*1024;
1320
1321         rga2_mmu_buf.pages = kmalloc(32768 * sizeof(struct page *), GFP_KERNEL);
1322
1323         if ((ret = platform_driver_register(&rga2_driver)) != 0)
1324         {
1325                 printk(KERN_ERR "Platform device register failed (%d).\n", ret);
1326                 return ret;
1327         }
1328
1329         rga2_session_global.pid = 0x0000ffff;
1330         INIT_LIST_HEAD(&rga2_session_global.waiting);
1331         INIT_LIST_HEAD(&rga2_session_global.running);
1332         INIT_LIST_HEAD(&rga2_session_global.list_session);
1333
1334         INIT_LIST_HEAD(&rga2_service.waiting);
1335         INIT_LIST_HEAD(&rga2_service.running);
1336         INIT_LIST_HEAD(&rga2_service.done);
1337         INIT_LIST_HEAD(&rga2_service.session);
1338         init_waitqueue_head(&rga2_session_global.wait);
1339         //mutex_lock(&rga_service.lock);
1340         list_add_tail(&rga2_session_global.list_session, &rga2_service.session);
1341         //mutex_unlock(&rga_service.lock);
1342         atomic_set(&rga2_session_global.task_running, 0);
1343         atomic_set(&rga2_session_global.num_done, 0);
1344
1345 #if RGA2_TEST_CASE
1346         rga2_test_0();
1347 #endif
1348
1349         INFO("Module initialized.\n");
1350
1351         return 0;
1352 }
1353
1354 static void __exit rga2_exit(void)
1355 {
1356         rga2_power_off();
1357
1358         if (rga2_mmu_buf.buf_virtual)
1359                 kfree(rga2_mmu_buf.buf_virtual);
1360
1361         platform_driver_unregister(&rga2_driver);
1362 }
1363
1364
1365 #if RGA2_TEST_CASE
1366
1367 void rga2_test_0(void)
1368 {
1369         struct rga2_req req;
1370         rga2_session session;
1371         unsigned int *src, *dst;
1372         uint32_t i, j;
1373         uint32_t *dst0;
1374
1375         session.pid     = current->pid;
1376         INIT_LIST_HEAD(&session.waiting);
1377         INIT_LIST_HEAD(&session.running);
1378         INIT_LIST_HEAD(&session.list_session);
1379         init_waitqueue_head(&session.wait);
1380         /* no need to protect */
1381         list_add_tail(&session.list_session, &rga2_service.session);
1382         atomic_set(&session.task_running, 0);
1383         atomic_set(&session.num_done, 0);
1384         //file->private_data = (void *)session;
1385
1386         //fb = rk_get_fb(0);
1387
1388         memset(&req, 0, sizeof(struct rga2_req));
1389         src = kmalloc(800*480*4, GFP_KERNEL);
1390         dst = kmalloc(800*480*4, GFP_KERNEL);
1391
1392         printk("\n********************************\n");
1393         printk("************ RGA2_TEST ************\n");
1394         printk("********************************\n\n");
1395
1396         memset(src, 0x80, 800*480*4);
1397         memset(dst, 0x0, 800*480*4);
1398
1399         //dmac_flush_range(&src, &src[800*480*4]);
1400         //outer_flush_range(virt_to_phys(&src),virt_to_phys(&src[800*480*4]));
1401
1402 #if 0
1403         memset(src_buf, 0x80, 800*480*4);
1404         memset(dst_buf, 0xcc, 800*480*4);
1405
1406         dmac_flush_range(&dst_buf[0], &dst_buf[800*480]);
1407         outer_flush_range(virt_to_phys(&dst_buf[0]),virt_to_phys(&dst_buf[800*480]));
1408 #endif
1409
1410         dst0 = (uint32_t *)&dst;
1411         i = j = 0;
1412 #if 0
1413         req.pat.act_w = 16;
1414         req.pat.act_h = 16;
1415         req.pat.vir_w = 16;
1416         req.pat.vir_h = 16;
1417         req.pat.yrgb_addr = virt_to_phys(src);
1418         req.render_mode = 0;
1419         rga2_blit_sync(&session, &req);
1420 #endif
1421
1422         req.src.act_w  = 320;
1423         req.src.act_h = 240;
1424
1425         req.src.vir_w  = 320;
1426         req.src.vir_h = 240;
1427         req.src.yrgb_addr = 0;//(uint32_t)virt_to_phys(src);
1428         req.src.uv_addr = (unsigned long)virt_to_phys(src);
1429         req.src.v_addr = 0;
1430         req.src.format = RGA2_FORMAT_RGBA_8888;
1431
1432         req.dst.act_w  = 320;
1433         req.dst.act_h = 240;
1434         req.dst.x_offset = 0;
1435         req.dst.y_offset = 0;
1436
1437         req.dst.vir_w = 320;
1438         req.dst.vir_h = 240;
1439
1440         req.dst.yrgb_addr = 0;//((uint32_t)virt_to_phys(dst));
1441         req.dst.uv_addr = (unsigned long)virt_to_phys(dst);
1442         req.dst.format = RGA2_FORMAT_RGBA_8888;
1443
1444         //dst = dst0;
1445
1446         //req.render_mode = color_fill_mode;
1447         //req.fg_color = 0x80ffffff;
1448
1449         req.rotate_mode = 0;
1450         req.scale_bicu_mode = 2;
1451
1452         //req.alpha_rop_flag = 0;
1453         //req.alpha_rop_mode = 0x19;
1454         //req.PD_mode = 3;
1455
1456         //req.mmu_info.mmu_flag = 0x21;
1457         //req.mmu_info.mmu_en = 1;
1458
1459         //printk("src = %.8x\n", req.src.yrgb_addr);
1460         //printk("src = %.8x\n", req.src.uv_addr);
1461         //printk("dst = %.8x\n", req.dst.yrgb_addr);
1462
1463         rga2_blit_sync(&session, &req);
1464
1465         for(j=0; j<100; j++) {
1466                 printk("%.8x\n", dst[j]);
1467         }
1468
1469         if(src)
1470                 kfree(src);
1471         if(dst)
1472                 kfree(dst);
1473 }
1474 #endif
1475
1476 module_init(rga2_init);
1477 module_exit(rga2_exit);
1478
1479 /* Module information */
1480 MODULE_AUTHOR("zsq@rock-chips.com");
1481 MODULE_DESCRIPTION("Driver for rga device");
1482 MODULE_LICENSE("GPL");