blob: 5e186a78a606c47899ac298573d2c68b52a1cf94 [file] [log] [blame]
Adrian Salido-Moreno19caf152012-01-03 18:46:25 -08001/* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13#include <linux/platform_device.h>
14#include <linux/cdev.h>
15#include <linux/list.h>
16#include <linux/module.h>
17#include <linux/fs.h>
18#include <linux/interrupt.h>
19#include <linux/sched.h>
20#include <linux/uaccess.h>
21#include <linux/clk.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022#include <linux/android_pmem.h>
23#include <linux/msm_rotator.h>
24#include <linux/io.h>
25#include <mach/msm_rotator_imem.h>
26#include <linux/ktime.h>
27#include <linux/workqueue.h>
28#include <linux/file.h>
29#include <linux/major.h>
30#include <linux/regulator/consumer.h>
Naseer Ahmed18018602011-10-25 13:32:58 -070031#include <linux/ion.h>
Nagamalleswararao Ganjie1a9f872011-11-06 23:13:32 -080032#ifdef CONFIG_MSM_BUS_SCALING
33#include <mach/msm_bus.h>
34#include <mach/msm_bus_board.h>
35#endif
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -070036#include <mach/msm_subsystem_map.h>
37#include <mach/iommu_domains.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038
39#define DRIVER_NAME "msm_rotator"
40
41#define MSM_ROTATOR_BASE (msm_rotator_dev->io_base)
42#define MSM_ROTATOR_INTR_ENABLE (MSM_ROTATOR_BASE+0x0020)
43#define MSM_ROTATOR_INTR_STATUS (MSM_ROTATOR_BASE+0x0024)
44#define MSM_ROTATOR_INTR_CLEAR (MSM_ROTATOR_BASE+0x0028)
45#define MSM_ROTATOR_START (MSM_ROTATOR_BASE+0x0030)
46#define MSM_ROTATOR_MAX_BURST_SIZE (MSM_ROTATOR_BASE+0x0050)
47#define MSM_ROTATOR_HW_VERSION (MSM_ROTATOR_BASE+0x0070)
48#define MSM_ROTATOR_SRC_SIZE (MSM_ROTATOR_BASE+0x1108)
49#define MSM_ROTATOR_SRCP0_ADDR (MSM_ROTATOR_BASE+0x110c)
50#define MSM_ROTATOR_SRCP1_ADDR (MSM_ROTATOR_BASE+0x1110)
Adrian Salido-Moreno0644f342011-07-08 12:05:01 -070051#define MSM_ROTATOR_SRCP2_ADDR (MSM_ROTATOR_BASE+0x1114)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052#define MSM_ROTATOR_SRC_YSTRIDE1 (MSM_ROTATOR_BASE+0x111c)
53#define MSM_ROTATOR_SRC_YSTRIDE2 (MSM_ROTATOR_BASE+0x1120)
54#define MSM_ROTATOR_SRC_FORMAT (MSM_ROTATOR_BASE+0x1124)
55#define MSM_ROTATOR_SRC_UNPACK_PATTERN1 (MSM_ROTATOR_BASE+0x1128)
56#define MSM_ROTATOR_SUB_BLOCK_CFG (MSM_ROTATOR_BASE+0x1138)
57#define MSM_ROTATOR_OUT_PACK_PATTERN1 (MSM_ROTATOR_BASE+0x1154)
58#define MSM_ROTATOR_OUTP0_ADDR (MSM_ROTATOR_BASE+0x1168)
59#define MSM_ROTATOR_OUTP1_ADDR (MSM_ROTATOR_BASE+0x116c)
Adrian Salido-Moreno0644f342011-07-08 12:05:01 -070060#define MSM_ROTATOR_OUTP2_ADDR (MSM_ROTATOR_BASE+0x1170)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061#define MSM_ROTATOR_OUT_YSTRIDE1 (MSM_ROTATOR_BASE+0x1178)
62#define MSM_ROTATOR_OUT_YSTRIDE2 (MSM_ROTATOR_BASE+0x117c)
63#define MSM_ROTATOR_SRC_XY (MSM_ROTATOR_BASE+0x1200)
64#define MSM_ROTATOR_SRC_IMAGE_SIZE (MSM_ROTATOR_BASE+0x1208)
65
66#define MSM_ROTATOR_MAX_ROT 0x07
67#define MSM_ROTATOR_MAX_H 0x1fff
68#define MSM_ROTATOR_MAX_W 0x1fff
69
70/* from lsb to msb */
71#define GET_PACK_PATTERN(a, x, y, z, bit) \
72 (((a)<<((bit)*3))|((x)<<((bit)*2))|((y)<<(bit))|(z))
73#define CLR_G 0x0
74#define CLR_B 0x1
75#define CLR_R 0x2
76#define CLR_ALPHA 0x3
77
78#define CLR_Y CLR_G
79#define CLR_CB CLR_B
80#define CLR_CR CLR_R
81
82#define ROTATIONS_TO_BITMASK(r) ((((r) & MDP_ROT_90) ? 1 : 0) | \
83 (((r) & MDP_FLIP_LR) ? 2 : 0) | \
84 (((r) & MDP_FLIP_UD) ? 4 : 0))
85
86#define IMEM_NO_OWNER -1;
87
88#define MAX_SESSIONS 16
89#define INVALID_SESSION -1
90#define VERSION_KEY_MASK 0xFFFFFF00
Adrian Salido-Moreno67273e52011-10-21 19:04:18 -070091#define MAX_DOWNSCALE_RATIO 3
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092
Mayank Chopra012a8e72012-04-11 10:41:13 +053093#define ROTATOR_REVISION_V0 0
94#define ROTATOR_REVISION_V1 1
95#define ROTATOR_REVISION_V2 2
96#define ROTATOR_REVISION_NONE 0xffffffff
97
98uint32_t rotator_hw_revision;
99
100/*
101 * rotator_hw_revision:
102 * 0 == 7x30
103 * 1 == 8x60
104 * 2 == 8960
105 *
106 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107struct tile_parm {
108 unsigned int width; /* tile's width */
109 unsigned int height; /* tile's height */
110 unsigned int row_tile_w; /* tiles per row's width */
111 unsigned int row_tile_h; /* tiles per row's height */
112};
113
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700114struct msm_rotator_mem_planes {
115 unsigned int num_planes;
116 unsigned int plane_size[4];
117 unsigned int total_size;
118};
119
120#define checkoffset(offset, size, max_size) \
121 ((size) > (max_size) || (offset) > ((max_size) - (size)))
122
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -0700123struct msm_rotator_fd_info {
124 int pid;
125 int ref_cnt;
126 struct list_head list;
127};
128
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700129struct msm_rotator_dev {
130 void __iomem *io_base;
131 int irq;
132 struct msm_rotator_img_info *img_info[MAX_SESSIONS];
133 struct clk *core_clk;
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -0700134 struct msm_rotator_fd_info *fd_info[MAX_SESSIONS];
135 struct list_head fd_list;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136 struct clk *pclk;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137 int rot_clk_state;
138 struct regulator *regulator;
139 struct delayed_work rot_clk_work;
140 struct clk *imem_clk;
141 int imem_clk_state;
142 struct delayed_work imem_clk_work;
143 struct platform_device *pdev;
144 struct cdev cdev;
145 struct device *device;
146 struct class *class;
147 dev_t dev_num;
148 int processing;
149 int last_session_idx;
150 struct mutex rotator_lock;
151 struct mutex imem_lock;
152 int imem_owner;
153 wait_queue_head_t wq;
Naseer Ahmed18018602011-10-25 13:32:58 -0700154 struct ion_client *client;
Nagamalleswararao Ganjie1a9f872011-11-06 23:13:32 -0800155 #ifdef CONFIG_MSM_BUS_SCALING
156 uint32_t bus_client_handle;
157 #endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158};
159
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160#define COMPONENT_5BITS 1
161#define COMPONENT_6BITS 2
162#define COMPONENT_8BITS 3
163
164static struct msm_rotator_dev *msm_rotator_dev;
165
166enum {
167 CLK_EN,
168 CLK_DIS,
169 CLK_SUSPEND,
170};
171
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700172int msm_rotator_iommu_map_buf(int mem_id, unsigned char src,
173 unsigned long *start, unsigned long *len,
174 struct ion_handle **pihdl)
175{
176 if (!msm_rotator_dev->client)
177 return -EINVAL;
178
179 *pihdl = ion_import_fd(msm_rotator_dev->client, mem_id);
180 if (IS_ERR_OR_NULL(*pihdl)) {
181 pr_err("ion_import_fd() failed\n");
182 return PTR_ERR(*pihdl);
183 }
184 pr_debug("%s(): ion_hdl %p, ion_buf %p\n", __func__, *pihdl,
185 ion_share(msm_rotator_dev->client, *pihdl));
186
187 if (ion_map_iommu(msm_rotator_dev->client,
188 *pihdl, ROTATOR_DOMAIN, GEN_POOL,
189 SZ_4K, 0, start, len, 0, ION_IOMMU_UNMAP_DELAYED)) {
190 pr_err("ion_map_iommu() failed\n");
191 return -EINVAL;
192 }
193
194 pr_debug("%s(): mem_id %d, start 0x%lx, len 0x%lx\n",
195 __func__, mem_id, *start, *len);
196 return 0;
197}
198
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199int msm_rotator_imem_allocate(int requestor)
200{
201 int rc = 0;
202
203#ifdef CONFIG_MSM_ROTATOR_USE_IMEM
204 switch (requestor) {
205 case ROTATOR_REQUEST:
206 if (mutex_trylock(&msm_rotator_dev->imem_lock)) {
207 msm_rotator_dev->imem_owner = ROTATOR_REQUEST;
208 rc = 1;
209 } else
210 rc = 0;
211 break;
212 case JPEG_REQUEST:
213 mutex_lock(&msm_rotator_dev->imem_lock);
214 msm_rotator_dev->imem_owner = JPEG_REQUEST;
215 rc = 1;
216 break;
217 default:
218 rc = 0;
219 }
220#else
221 if (requestor == JPEG_REQUEST)
222 rc = 1;
223#endif
224 if (rc == 1) {
225 cancel_delayed_work(&msm_rotator_dev->imem_clk_work);
226 if (msm_rotator_dev->imem_clk_state != CLK_EN
227 && msm_rotator_dev->imem_clk) {
Ravishangar Kalyanamd16485d2012-03-21 16:51:26 -0700228 clk_prepare_enable(msm_rotator_dev->imem_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229 msm_rotator_dev->imem_clk_state = CLK_EN;
230 }
231 }
232
233 return rc;
234}
235EXPORT_SYMBOL(msm_rotator_imem_allocate);
236
237void msm_rotator_imem_free(int requestor)
238{
239#ifdef CONFIG_MSM_ROTATOR_USE_IMEM
240 if (msm_rotator_dev->imem_owner == requestor) {
241 schedule_delayed_work(&msm_rotator_dev->imem_clk_work, HZ);
242 mutex_unlock(&msm_rotator_dev->imem_lock);
243 }
244#else
245 if (requestor == JPEG_REQUEST)
246 schedule_delayed_work(&msm_rotator_dev->imem_clk_work, HZ);
247#endif
248}
249EXPORT_SYMBOL(msm_rotator_imem_free);
250
251static void msm_rotator_imem_clk_work_f(struct work_struct *work)
252{
253#ifdef CONFIG_MSM_ROTATOR_USE_IMEM
254 if (mutex_trylock(&msm_rotator_dev->imem_lock)) {
255 if (msm_rotator_dev->imem_clk_state == CLK_EN
256 && msm_rotator_dev->imem_clk) {
Ravishangar Kalyanamd16485d2012-03-21 16:51:26 -0700257 clk_disable_unprepare(msm_rotator_dev->imem_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258 msm_rotator_dev->imem_clk_state = CLK_DIS;
259 } else if (msm_rotator_dev->imem_clk_state == CLK_SUSPEND)
260 msm_rotator_dev->imem_clk_state = CLK_DIS;
261 mutex_unlock(&msm_rotator_dev->imem_lock);
262 }
263#endif
264}
265
266/* enable clocks needed by rotator block */
267static void enable_rot_clks(void)
268{
269 if (msm_rotator_dev->regulator)
270 regulator_enable(msm_rotator_dev->regulator);
271 if (msm_rotator_dev->core_clk != NULL)
Ravishangar Kalyanamd16485d2012-03-21 16:51:26 -0700272 clk_prepare_enable(msm_rotator_dev->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700273 if (msm_rotator_dev->pclk != NULL)
Ravishangar Kalyanamd16485d2012-03-21 16:51:26 -0700274 clk_prepare_enable(msm_rotator_dev->pclk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275}
276
277/* disable clocks needed by rotator block */
278static void disable_rot_clks(void)
279{
280 if (msm_rotator_dev->core_clk != NULL)
Ravishangar Kalyanamd16485d2012-03-21 16:51:26 -0700281 clk_disable_unprepare(msm_rotator_dev->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700282 if (msm_rotator_dev->pclk != NULL)
Ravishangar Kalyanamd16485d2012-03-21 16:51:26 -0700283 clk_disable_unprepare(msm_rotator_dev->pclk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700284 if (msm_rotator_dev->regulator)
285 regulator_disable(msm_rotator_dev->regulator);
286}
287
288static void msm_rotator_rot_clk_work_f(struct work_struct *work)
289{
290 if (mutex_trylock(&msm_rotator_dev->rotator_lock)) {
291 if (msm_rotator_dev->rot_clk_state == CLK_EN) {
292 disable_rot_clks();
293 msm_rotator_dev->rot_clk_state = CLK_DIS;
294 } else if (msm_rotator_dev->rot_clk_state == CLK_SUSPEND)
295 msm_rotator_dev->rot_clk_state = CLK_DIS;
296 mutex_unlock(&msm_rotator_dev->rotator_lock);
297 }
298}
299
300static irqreturn_t msm_rotator_isr(int irq, void *dev_id)
301{
302 if (msm_rotator_dev->processing) {
303 msm_rotator_dev->processing = 0;
304 wake_up(&msm_rotator_dev->wq);
305 } else
306 printk(KERN_WARNING "%s: unexpected interrupt\n", DRIVER_NAME);
307
308 return IRQ_HANDLED;
309}
310
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700311static unsigned int tile_size(unsigned int src_width,
312 unsigned int src_height,
313 const struct tile_parm *tp)
314{
315 unsigned int tile_w, tile_h;
316 unsigned int row_num_w, row_num_h;
317 tile_w = tp->width * tp->row_tile_w;
318 tile_h = tp->height * tp->row_tile_h;
319 row_num_w = (src_width + tile_w - 1) / tile_w;
320 row_num_h = (src_height + tile_h - 1) / tile_h;
321 return ((row_num_w * row_num_h * tile_w * tile_h) + 8191) & ~8191;
322}
323
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324static int get_bpp(int format)
325{
326 switch (format) {
327 case MDP_RGB_565:
328 case MDP_BGR_565:
329 return 2;
330
331 case MDP_XRGB_8888:
332 case MDP_ARGB_8888:
333 case MDP_RGBA_8888:
334 case MDP_BGRA_8888:
335 case MDP_RGBX_8888:
336 return 4;
337
338 case MDP_Y_CBCR_H2V2:
339 case MDP_Y_CRCB_H2V2:
Adrian Salido-Moreno0644f342011-07-08 12:05:01 -0700340 case MDP_Y_CB_CR_H2V2:
341 case MDP_Y_CR_CB_H2V2:
Pradeep Jilagam9b4a6be2011-10-03 17:19:20 +0530342 case MDP_Y_CR_CB_GH2V2:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343 case MDP_Y_CRCB_H2V2_TILE:
344 case MDP_Y_CBCR_H2V2_TILE:
345 return 1;
346
347 case MDP_RGB_888:
348 return 3;
349
350 case MDP_YCRYCB_H2V1:
351 return 2;/* YCrYCb interleave */
352
353 case MDP_Y_CRCB_H2V1:
354 case MDP_Y_CBCR_H2V1:
355 return 1;
356
357 default:
358 return -1;
359 }
360
361}
362
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700363static int msm_rotator_get_plane_sizes(uint32_t format, uint32_t w, uint32_t h,
364 struct msm_rotator_mem_planes *p)
365{
366 /*
367 * each row of samsung tile consists of two tiles in height
368 * and two tiles in width which means width should align to
369 * 64 x 2 bytes and height should align to 32 x 2 bytes.
370 * video decoder generate two tiles in width and one tile
371 * in height which ends up height align to 32 X 1 bytes.
372 */
373 const struct tile_parm tile = {64, 32, 2, 1};
374 int i;
375
376 if (p == NULL)
377 return -EINVAL;
378
379 if ((w > MSM_ROTATOR_MAX_W) || (h > MSM_ROTATOR_MAX_H))
380 return -ERANGE;
381
382 memset(p, 0, sizeof(*p));
383
384 switch (format) {
385 case MDP_XRGB_8888:
386 case MDP_ARGB_8888:
387 case MDP_RGBA_8888:
388 case MDP_BGRA_8888:
389 case MDP_RGBX_8888:
390 case MDP_RGB_888:
391 case MDP_RGB_565:
392 case MDP_BGR_565:
393 case MDP_YCRYCB_H2V1:
394 p->num_planes = 1;
395 p->plane_size[0] = w * h * get_bpp(format);
396 break;
397 case MDP_Y_CRCB_H2V1:
398 case MDP_Y_CBCR_H2V1:
399 p->num_planes = 2;
400 p->plane_size[0] = w * h;
401 p->plane_size[1] = w * h;
402 break;
403 case MDP_Y_CBCR_H2V2:
404 case MDP_Y_CRCB_H2V2:
405 p->num_planes = 2;
406 p->plane_size[0] = w * h;
407 p->plane_size[1] = w * h / 2;
408 break;
409 case MDP_Y_CRCB_H2V2_TILE:
410 case MDP_Y_CBCR_H2V2_TILE:
411 p->num_planes = 2;
412 p->plane_size[0] = tile_size(w, h, &tile);
413 p->plane_size[1] = tile_size(w, h/2, &tile);
414 break;
415 case MDP_Y_CB_CR_H2V2:
416 case MDP_Y_CR_CB_H2V2:
417 p->num_planes = 3;
418 p->plane_size[0] = w * h;
419 p->plane_size[1] = (w / 2) * (h / 2);
420 p->plane_size[2] = (w / 2) * (h / 2);
421 break;
422 case MDP_Y_CR_CB_GH2V2:
423 p->num_planes = 3;
424 p->plane_size[0] = ALIGN(w, 16) * h;
425 p->plane_size[1] = ALIGN(w / 2, 16) * (h / 2);
426 p->plane_size[2] = ALIGN(w / 2, 16) * (h / 2);
427 break;
428 default:
429 return -EINVAL;
430 }
431
432 for (i = 0; i < p->num_planes; i++)
433 p->total_size += p->plane_size[i];
434
435 return 0;
436}
437
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438static int msm_rotator_ycxcx_h2v1(struct msm_rotator_img_info *info,
439 unsigned int in_paddr,
440 unsigned int out_paddr,
441 unsigned int use_imem,
442 int new_session,
443 unsigned int in_chroma_paddr,
444 unsigned int out_chroma_paddr)
445{
446 int bpp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447
448 if (info->src.format != info->dst.format)
449 return -EINVAL;
450
451 bpp = get_bpp(info->src.format);
452 if (bpp < 0)
453 return -ENOTTY;
454
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455 iowrite32(in_paddr, MSM_ROTATOR_SRCP0_ADDR);
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700456 iowrite32(in_chroma_paddr, MSM_ROTATOR_SRCP1_ADDR);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457 iowrite32(out_paddr +
458 ((info->dst_y * info->dst.width) + info->dst_x),
459 MSM_ROTATOR_OUTP0_ADDR);
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700460 iowrite32(out_chroma_paddr +
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461 ((info->dst_y * info->dst.width) + info->dst_x),
462 MSM_ROTATOR_OUTP1_ADDR);
463
464 if (new_session) {
465 iowrite32(info->src.width |
466 info->src.width << 16,
467 MSM_ROTATOR_SRC_YSTRIDE1);
468 if (info->rotations & MDP_ROT_90)
469 iowrite32(info->dst.width |
470 info->dst.width*2 << 16,
471 MSM_ROTATOR_OUT_YSTRIDE1);
472 else
473 iowrite32(info->dst.width |
474 info->dst.width << 16,
475 MSM_ROTATOR_OUT_YSTRIDE1);
476 if (info->src.format == MDP_Y_CBCR_H2V1) {
477 iowrite32(GET_PACK_PATTERN(0, 0, CLR_CB, CLR_CR, 8),
478 MSM_ROTATOR_SRC_UNPACK_PATTERN1);
479 iowrite32(GET_PACK_PATTERN(0, 0, CLR_CB, CLR_CR, 8),
480 MSM_ROTATOR_OUT_PACK_PATTERN1);
481 } else {
482 iowrite32(GET_PACK_PATTERN(0, 0, CLR_CR, CLR_CB, 8),
483 MSM_ROTATOR_SRC_UNPACK_PATTERN1);
484 iowrite32(GET_PACK_PATTERN(0, 0, CLR_CR, CLR_CB, 8),
485 MSM_ROTATOR_OUT_PACK_PATTERN1);
486 }
487 iowrite32((1 << 18) | /* chroma sampling 1=H2V1 */
488 (ROTATIONS_TO_BITMASK(info->rotations) << 9) |
Adrian Salido-Moreno67273e52011-10-21 19:04:18 -0700489 1 << 8 | /* ROT_EN */
490 info->downscale_ratio << 2 | /* downscale v ratio */
491 info->downscale_ratio, /* downscale h ratio */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 MSM_ROTATOR_SUB_BLOCK_CFG);
493 iowrite32(0 << 29 | /* frame format 0 = linear */
494 (use_imem ? 0 : 1) << 22 | /* tile size */
495 2 << 19 | /* fetch planes 2 = pseudo */
496 0 << 18 | /* unpack align */
497 1 << 17 | /* unpack tight */
498 1 << 13 | /* unpack count 0=1 component */
499 (bpp-1) << 9 | /* src Bpp 0=1 byte ... */
500 0 << 8 | /* has alpha */
501 0 << 6 | /* alpha bits 3=8bits */
502 3 << 4 | /* R/Cr bits 1=5 2=6 3=8 */
503 3 << 2 | /* B/Cb bits 1=5 2=6 3=8 */
504 3 << 0, /* G/Y bits 1=5 2=6 3=8 */
505 MSM_ROTATOR_SRC_FORMAT);
506 }
507
508 return 0;
509}
510
511static int msm_rotator_ycxcx_h2v2(struct msm_rotator_img_info *info,
512 unsigned int in_paddr,
513 unsigned int out_paddr,
514 unsigned int use_imem,
515 int new_session,
516 unsigned int in_chroma_paddr,
Adrian Salido-Moreno0644f342011-07-08 12:05:01 -0700517 unsigned int out_chroma_paddr,
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700518 unsigned int in_chroma2_paddr)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519{
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700520 uint32_t dst_format;
521 int is_tile = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700523 switch (info->src.format) {
524 case MDP_Y_CRCB_H2V2_TILE:
525 is_tile = 1;
526 case MDP_Y_CR_CB_H2V2:
527 case MDP_Y_CR_CB_GH2V2:
528 case MDP_Y_CRCB_H2V2:
529 dst_format = MDP_Y_CRCB_H2V2;
530 break;
531 case MDP_Y_CBCR_H2V2_TILE:
532 is_tile = 1;
533 case MDP_Y_CB_CR_H2V2:
534 case MDP_Y_CBCR_H2V2:
535 dst_format = MDP_Y_CBCR_H2V2;
536 break;
537 default:
538 return -EINVAL;
539 }
540 if (info->dst.format != dst_format)
541 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542
Adrian Salido-Moreno19caf152012-01-03 18:46:25 -0800543 /* rotator expects YCbCr for planar input format */
Mayank Chopra012a8e72012-04-11 10:41:13 +0530544 if ((info->src.format == MDP_Y_CR_CB_H2V2 ||
545 info->src.format == MDP_Y_CR_CB_GH2V2) &&
546 rotator_hw_revision < ROTATOR_REVISION_V2)
Adrian Salido-Moreno19caf152012-01-03 18:46:25 -0800547 swap(in_chroma_paddr, in_chroma2_paddr);
548
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549 iowrite32(in_paddr, MSM_ROTATOR_SRCP0_ADDR);
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700550 iowrite32(in_chroma_paddr, MSM_ROTATOR_SRCP1_ADDR);
551 iowrite32(in_chroma2_paddr, MSM_ROTATOR_SRCP2_ADDR);
552
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553 iowrite32(out_paddr +
554 ((info->dst_y * info->dst.width) + info->dst_x),
555 MSM_ROTATOR_OUTP0_ADDR);
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700556 iowrite32(out_chroma_paddr +
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 ((info->dst_y * info->dst.width)/2 + info->dst_x),
558 MSM_ROTATOR_OUTP1_ADDR);
559
560 if (new_session) {
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700561 if (in_chroma2_paddr) {
562 if (info->src.format == MDP_Y_CR_CB_GH2V2) {
Pradeep Jilagam9b4a6be2011-10-03 17:19:20 +0530563 iowrite32(ALIGN(info->src.width, 16) |
564 ALIGN((info->src.width / 2), 16) << 16,
565 MSM_ROTATOR_SRC_YSTRIDE1);
566 iowrite32(ALIGN((info->src.width / 2), 16),
567 MSM_ROTATOR_SRC_YSTRIDE2);
568 } else {
569 iowrite32(info->src.width |
Adrian Salido-Moreno0644f342011-07-08 12:05:01 -0700570 (info->src.width / 2) << 16,
571 MSM_ROTATOR_SRC_YSTRIDE1);
Pradeep Jilagam9b4a6be2011-10-03 17:19:20 +0530572 iowrite32((info->src.width / 2),
Adrian Salido-Moreno0644f342011-07-08 12:05:01 -0700573 MSM_ROTATOR_SRC_YSTRIDE2);
Pradeep Jilagam9b4a6be2011-10-03 17:19:20 +0530574 }
Adrian Salido-Moreno0644f342011-07-08 12:05:01 -0700575 } else {
576 iowrite32(info->src.width |
577 info->src.width << 16,
578 MSM_ROTATOR_SRC_YSTRIDE1);
579 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580 iowrite32(info->dst.width |
Adrian Salido-Moreno0644f342011-07-08 12:05:01 -0700581 info->dst.width << 16,
582 MSM_ROTATOR_OUT_YSTRIDE1);
583
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700584 if (dst_format == MDP_Y_CBCR_H2V2) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585 iowrite32(GET_PACK_PATTERN(0, 0, CLR_CB, CLR_CR, 8),
586 MSM_ROTATOR_SRC_UNPACK_PATTERN1);
587 iowrite32(GET_PACK_PATTERN(0, 0, CLR_CB, CLR_CR, 8),
588 MSM_ROTATOR_OUT_PACK_PATTERN1);
589 } else {
590 iowrite32(GET_PACK_PATTERN(0, 0, CLR_CR, CLR_CB, 8),
591 MSM_ROTATOR_SRC_UNPACK_PATTERN1);
592 iowrite32(GET_PACK_PATTERN(0, 0, CLR_CR, CLR_CB, 8),
593 MSM_ROTATOR_OUT_PACK_PATTERN1);
594 }
595 iowrite32((3 << 18) | /* chroma sampling 3=4:2:0 */
596 (ROTATIONS_TO_BITMASK(info->rotations) << 9) |
Adrian Salido-Moreno67273e52011-10-21 19:04:18 -0700597 1 << 8 | /* ROT_EN */
598 info->downscale_ratio << 2 | /* downscale v ratio */
599 info->downscale_ratio, /* downscale h ratio */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 MSM_ROTATOR_SUB_BLOCK_CFG);
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700601
602 iowrite32((is_tile ? 2 : 0) << 29 | /* frame format */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 (use_imem ? 0 : 1) << 22 | /* tile size */
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700604 (in_chroma2_paddr ? 1 : 2) << 19 | /* fetch planes */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605 0 << 18 | /* unpack align */
606 1 << 17 | /* unpack tight */
607 1 << 13 | /* unpack count 0=1 component */
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700608 0 << 9 | /* src Bpp 0=1 byte ... */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609 0 << 8 | /* has alpha */
610 0 << 6 | /* alpha bits 3=8bits */
611 3 << 4 | /* R/Cr bits 1=5 2=6 3=8 */
612 3 << 2 | /* B/Cb bits 1=5 2=6 3=8 */
613 3 << 0, /* G/Y bits 1=5 2=6 3=8 */
614 MSM_ROTATOR_SRC_FORMAT);
615 }
616 return 0;
617}
618
619static int msm_rotator_ycrycb(struct msm_rotator_img_info *info,
620 unsigned int in_paddr,
621 unsigned int out_paddr,
622 unsigned int use_imem,
Mayank Chopra732dcd62012-01-09 20:53:39 +0530623 int new_session,
624 unsigned int out_chroma_paddr)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625{
626 int bpp;
Mayank Chopra732dcd62012-01-09 20:53:39 +0530627 uint32_t dst_format;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628
Mayank Chopra732dcd62012-01-09 20:53:39 +0530629 if (info->src.format == MDP_YCRYCB_H2V1)
630 dst_format = MDP_Y_CRCB_H2V1;
631 else
632 return -EINVAL;
633
634 if (info->dst.format != dst_format)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635 return -EINVAL;
636
637 bpp = get_bpp(info->src.format);
638 if (bpp < 0)
639 return -ENOTTY;
640
641 iowrite32(in_paddr, MSM_ROTATOR_SRCP0_ADDR);
642 iowrite32(out_paddr +
643 ((info->dst_y * info->dst.width) + info->dst_x),
644 MSM_ROTATOR_OUTP0_ADDR);
Mayank Chopra732dcd62012-01-09 20:53:39 +0530645 iowrite32(out_chroma_paddr +
646 ((info->dst_y * info->dst.width)/2 + info->dst_x),
647 MSM_ROTATOR_OUTP1_ADDR);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648
649 if (new_session) {
Mayank Chopra732dcd62012-01-09 20:53:39 +0530650 iowrite32(info->src.width * bpp,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 MSM_ROTATOR_SRC_YSTRIDE1);
Mayank Chopra732dcd62012-01-09 20:53:39 +0530652 if (info->rotations & MDP_ROT_90)
653 iowrite32(info->dst.width |
654 (info->dst.width*2) << 16,
655 MSM_ROTATOR_OUT_YSTRIDE1);
656 else
657 iowrite32(info->dst.width |
658 (info->dst.width) << 16,
659 MSM_ROTATOR_OUT_YSTRIDE1);
660
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661 iowrite32(GET_PACK_PATTERN(CLR_Y, CLR_CR, CLR_Y, CLR_CB, 8),
662 MSM_ROTATOR_SRC_UNPACK_PATTERN1);
Mayank Chopra732dcd62012-01-09 20:53:39 +0530663 iowrite32(GET_PACK_PATTERN(0, 0, CLR_CR, CLR_CB, 8),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 MSM_ROTATOR_OUT_PACK_PATTERN1);
665 iowrite32((1 << 18) | /* chroma sampling 1=H2V1 */
666 (ROTATIONS_TO_BITMASK(info->rotations) << 9) |
Adrian Salido-Moreno67273e52011-10-21 19:04:18 -0700667 1 << 8 | /* ROT_EN */
668 info->downscale_ratio << 2 | /* downscale v ratio */
669 info->downscale_ratio, /* downscale h ratio */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700670 MSM_ROTATOR_SUB_BLOCK_CFG);
671 iowrite32(0 << 29 | /* frame format 0 = linear */
672 (use_imem ? 0 : 1) << 22 | /* tile size */
673 0 << 19 | /* fetch planes 0=interleaved */
674 0 << 18 | /* unpack align */
675 1 << 17 | /* unpack tight */
676 3 << 13 | /* unpack count 0=1 component */
677 (bpp-1) << 9 | /* src Bpp 0=1 byte ... */
678 0 << 8 | /* has alpha */
679 0 << 6 | /* alpha bits 3=8bits */
680 3 << 4 | /* R/Cr bits 1=5 2=6 3=8 */
681 3 << 2 | /* B/Cb bits 1=5 2=6 3=8 */
682 3 << 0, /* G/Y bits 1=5 2=6 3=8 */
683 MSM_ROTATOR_SRC_FORMAT);
684 }
685
686 return 0;
687}
688
689static int msm_rotator_rgb_types(struct msm_rotator_img_info *info,
690 unsigned int in_paddr,
691 unsigned int out_paddr,
692 unsigned int use_imem,
693 int new_session)
694{
695 int bpp, abits, rbits, gbits, bbits;
696
697 if (info->src.format != info->dst.format)
698 return -EINVAL;
699
700 bpp = get_bpp(info->src.format);
701 if (bpp < 0)
702 return -ENOTTY;
703
704 iowrite32(in_paddr, MSM_ROTATOR_SRCP0_ADDR);
705 iowrite32(out_paddr +
706 ((info->dst_y * info->dst.width) + info->dst_x) * bpp,
707 MSM_ROTATOR_OUTP0_ADDR);
708
709 if (new_session) {
710 iowrite32(info->src.width * bpp, MSM_ROTATOR_SRC_YSTRIDE1);
711 iowrite32(info->dst.width * bpp, MSM_ROTATOR_OUT_YSTRIDE1);
712 iowrite32((0 << 18) | /* chroma sampling 0=rgb */
713 (ROTATIONS_TO_BITMASK(info->rotations) << 9) |
Adrian Salido-Moreno67273e52011-10-21 19:04:18 -0700714 1 << 8 | /* ROT_EN */
715 info->downscale_ratio << 2 | /* downscale v ratio */
716 info->downscale_ratio, /* downscale h ratio */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 MSM_ROTATOR_SUB_BLOCK_CFG);
718 switch (info->src.format) {
719 case MDP_RGB_565:
720 iowrite32(GET_PACK_PATTERN(0, CLR_R, CLR_G, CLR_B, 8),
721 MSM_ROTATOR_SRC_UNPACK_PATTERN1);
722 iowrite32(GET_PACK_PATTERN(0, CLR_R, CLR_G, CLR_B, 8),
723 MSM_ROTATOR_OUT_PACK_PATTERN1);
724 abits = 0;
725 rbits = COMPONENT_5BITS;
726 gbits = COMPONENT_6BITS;
727 bbits = COMPONENT_5BITS;
728 break;
729
730 case MDP_BGR_565:
731 iowrite32(GET_PACK_PATTERN(0, CLR_B, CLR_G, CLR_R, 8),
732 MSM_ROTATOR_SRC_UNPACK_PATTERN1);
733 iowrite32(GET_PACK_PATTERN(0, CLR_B, CLR_G, CLR_R, 8),
734 MSM_ROTATOR_OUT_PACK_PATTERN1);
735 abits = 0;
736 rbits = COMPONENT_5BITS;
737 gbits = COMPONENT_6BITS;
738 bbits = COMPONENT_5BITS;
739 break;
740
741 case MDP_RGB_888:
742 iowrite32(GET_PACK_PATTERN(0, CLR_R, CLR_G, CLR_B, 8),
743 MSM_ROTATOR_SRC_UNPACK_PATTERN1);
744 iowrite32(GET_PACK_PATTERN(0, CLR_R, CLR_G, CLR_B, 8),
745 MSM_ROTATOR_OUT_PACK_PATTERN1);
746 abits = 0;
747 rbits = COMPONENT_8BITS;
748 gbits = COMPONENT_8BITS;
749 bbits = COMPONENT_8BITS;
750 break;
751
752 case MDP_ARGB_8888:
753 case MDP_RGBA_8888:
754 case MDP_XRGB_8888:
755 case MDP_RGBX_8888:
756 iowrite32(GET_PACK_PATTERN(CLR_ALPHA, CLR_R, CLR_G,
757 CLR_B, 8),
758 MSM_ROTATOR_SRC_UNPACK_PATTERN1);
759 iowrite32(GET_PACK_PATTERN(CLR_ALPHA, CLR_R, CLR_G,
760 CLR_B, 8),
761 MSM_ROTATOR_OUT_PACK_PATTERN1);
762 abits = COMPONENT_8BITS;
763 rbits = COMPONENT_8BITS;
764 gbits = COMPONENT_8BITS;
765 bbits = COMPONENT_8BITS;
766 break;
767
768 case MDP_BGRA_8888:
769 iowrite32(GET_PACK_PATTERN(CLR_ALPHA, CLR_B, CLR_G,
770 CLR_R, 8),
771 MSM_ROTATOR_SRC_UNPACK_PATTERN1);
772 iowrite32(GET_PACK_PATTERN(CLR_ALPHA, CLR_B, CLR_G,
773 CLR_R, 8),
774 MSM_ROTATOR_OUT_PACK_PATTERN1);
775 abits = COMPONENT_8BITS;
776 rbits = COMPONENT_8BITS;
777 gbits = COMPONENT_8BITS;
778 bbits = COMPONENT_8BITS;
779 break;
780
781 default:
782 return -EINVAL;
783 }
784 iowrite32(0 << 29 | /* frame format 0 = linear */
785 (use_imem ? 0 : 1) << 22 | /* tile size */
786 0 << 19 | /* fetch planes 0=interleaved */
787 0 << 18 | /* unpack align */
788 1 << 17 | /* unpack tight */
789 (abits ? 3 : 2) << 13 | /* unpack count 0=1 comp */
790 (bpp-1) << 9 | /* src Bpp 0=1 byte ... */
791 (abits ? 1 : 0) << 8 | /* has alpha */
792 abits << 6 | /* alpha bits 3=8bits */
793 rbits << 4 | /* R/Cr bits 1=5 2=6 3=8 */
794 bbits << 2 | /* B/Cb bits 1=5 2=6 3=8 */
795 gbits << 0, /* G/Y bits 1=5 2=6 3=8 */
796 MSM_ROTATOR_SRC_FORMAT);
797 }
798
799 return 0;
800}
801
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700802static int get_img(struct msmfb_data *fbd, unsigned char src,
803 unsigned long *start, unsigned long *len, struct file **p_file,
804 int *p_need, struct ion_handle **p_ihdl)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700805{
806 int ret = 0;
807#ifdef CONFIG_FB
Naseer Ahmed18018602011-10-25 13:32:58 -0700808 struct file *file = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700809 int put_needed, fb_num;
810#endif
811#ifdef CONFIG_ANDROID_PMEM
812 unsigned long vstart;
813#endif
814
Kuogee Hsiehbeaa9352012-02-24 16:59:26 -0800815 *p_need = 0;
816
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817#ifdef CONFIG_FB
Naseer Ahmed18018602011-10-25 13:32:58 -0700818 if (fbd->flags & MDP_MEMORY_ID_TYPE_FB) {
819 file = fget_light(fbd->memory_id, &put_needed);
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700820 if (file == NULL) {
821 pr_err("fget_light returned NULL\n");
Naseer Ahmed18018602011-10-25 13:32:58 -0700822 return -EINVAL;
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700823 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824
Naseer Ahmed18018602011-10-25 13:32:58 -0700825 if (MAJOR(file->f_dentry->d_inode->i_rdev) == FB_MAJOR) {
826 fb_num = MINOR(file->f_dentry->d_inode->i_rdev);
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700827 if (get_fb_phys_info(start, len, fb_num,
828 ROTATOR_SUBSYSTEM_ID)) {
829 pr_err("get_fb_phys_info() failed\n");
Naseer Ahmed18018602011-10-25 13:32:58 -0700830 ret = -1;
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700831 } else {
Naseer Ahmed18018602011-10-25 13:32:58 -0700832 *p_file = file;
Kuogee Hsiehbeaa9352012-02-24 16:59:26 -0800833 *p_need = put_needed;
834 }
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700835 } else {
836 pr_err("invalid FB_MAJOR failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837 ret = -1;
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700838 }
Naseer Ahmed18018602011-10-25 13:32:58 -0700839 if (ret)
840 fput_light(file, put_needed);
841 return ret;
842 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843#endif
Naseer Ahmed18018602011-10-25 13:32:58 -0700844
845#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700846 return msm_rotator_iommu_map_buf(fbd->memory_id, src, start,
847 len, p_ihdl);
Naseer Ahmed18018602011-10-25 13:32:58 -0700848#endif
849#ifdef CONFIG_ANDROID_PMEM
850 if (!get_pmem_file(fbd->memory_id, start, &vstart, len, p_file))
851 return 0;
852 else
853 return -ENOMEM;
854#endif
855
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700856}
857
Naseer Ahmed18018602011-10-25 13:32:58 -0700858static void put_img(struct file *p_file, struct ion_handle *p_ihdl)
859{
860#ifdef CONFIG_ANDROID_PMEM
861 if (p_file != NULL)
862 put_pmem_file(p_file);
863#endif
864#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700865 if (!IS_ERR_OR_NULL(p_ihdl)) {
866 pr_debug("%s(): p_ihdl %p\n", __func__, p_ihdl);
867 ion_unmap_iommu(msm_rotator_dev->client,
868 p_ihdl, ROTATOR_DOMAIN, GEN_POOL);
869
Naseer Ahmed18018602011-10-25 13:32:58 -0700870 ion_free(msm_rotator_dev->client, p_ihdl);
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700871 }
Naseer Ahmed18018602011-10-25 13:32:58 -0700872#endif
873}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874static int msm_rotator_do_rotate(unsigned long arg)
875{
Naseer Ahmed18018602011-10-25 13:32:58 -0700876 unsigned int status, format;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700877 struct msm_rotator_data_info info;
878 unsigned int in_paddr, out_paddr;
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700879 unsigned long src_len, dst_len;
Naseer Ahmed18018602011-10-25 13:32:58 -0700880 int use_imem = 0, rc = 0, s;
881 struct file *srcp0_file = NULL, *dstp0_file = NULL;
882 struct file *srcp1_file = NULL, *dstp1_file = NULL;
883 struct ion_handle *srcp0_ihdl = NULL, *dstp0_ihdl = NULL;
884 struct ion_handle *srcp1_ihdl = NULL, *dstp1_ihdl = NULL;
Kuogee Hsiehbeaa9352012-02-24 16:59:26 -0800885 int ps0_need, p_need;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700886 unsigned int in_chroma_paddr = 0, out_chroma_paddr = 0;
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700887 unsigned int in_chroma2_paddr = 0;
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700888 struct msm_rotator_img_info *img_info;
889 struct msm_rotator_mem_planes src_planes, dst_planes;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890
891 if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
892 return -EFAULT;
893
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894 mutex_lock(&msm_rotator_dev->rotator_lock);
895 for (s = 0; s < MAX_SESSIONS; s++)
896 if ((msm_rotator_dev->img_info[s] != NULL) &&
897 (info.session_id ==
898 (unsigned int)msm_rotator_dev->img_info[s]
899 ))
900 break;
901
902 if (s == MAX_SESSIONS) {
903 dev_dbg(msm_rotator_dev->device,
904 "%s() : Attempt to use invalid session_id %d\n",
905 __func__, s);
906 rc = -EINVAL;
907 goto do_rotate_unlock_mutex;
908 }
909
910 if (msm_rotator_dev->img_info[s]->enable == 0) {
911 dev_dbg(msm_rotator_dev->device,
912 "%s() : Session_id %d not enabled \n",
913 __func__, s);
914 rc = -EINVAL;
915 goto do_rotate_unlock_mutex;
916 }
917
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700918 img_info = msm_rotator_dev->img_info[s];
919 if (msm_rotator_get_plane_sizes(img_info->src.format,
920 img_info->src.width,
921 img_info->src.height,
922 &src_planes)) {
923 pr_err("%s: invalid src format\n", __func__);
924 rc = -EINVAL;
925 goto do_rotate_unlock_mutex;
926 }
927 if (msm_rotator_get_plane_sizes(img_info->dst.format,
928 img_info->dst.width,
929 img_info->dst.height,
930 &dst_planes)) {
931 pr_err("%s: invalid dst format\n", __func__);
932 rc = -EINVAL;
933 goto do_rotate_unlock_mutex;
934 }
935
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700936 rc = get_img(&info.src, 1, (unsigned long *)&in_paddr,
937 (unsigned long *)&src_len, &srcp0_file, &ps0_need,
938 &srcp0_ihdl);
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700939 if (rc) {
940 pr_err("%s: in get_img() failed id=0x%08x\n",
941 DRIVER_NAME, info.src.memory_id);
942 goto do_rotate_unlock_mutex;
943 }
944
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700945 rc = get_img(&info.dst, 0, (unsigned long *)&out_paddr,
946 (unsigned long *)&dst_len, &dstp0_file, &p_need,
947 &dstp0_ihdl);
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700948 if (rc) {
949 pr_err("%s: out get_img() failed id=0x%08x\n",
950 DRIVER_NAME, info.dst.memory_id);
951 goto do_rotate_unlock_mutex;
952 }
953
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954 format = msm_rotator_dev->img_info[s]->src.format;
955 if (((info.version_key & VERSION_KEY_MASK) == 0xA5B4C300) &&
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700956 ((info.version_key & ~VERSION_KEY_MASK) > 0) &&
957 (src_planes.num_planes == 2)) {
958 if (checkoffset(info.src.offset,
959 src_planes.plane_size[0],
960 src_len)) {
961 pr_err("%s: invalid src buffer (len=%lu offset=%x)\n",
962 __func__, src_len, info.src.offset);
963 rc = -ERANGE;
964 goto do_rotate_unlock_mutex;
965 }
966 if (checkoffset(info.dst.offset,
967 dst_planes.plane_size[0],
968 dst_len)) {
969 pr_err("%s: invalid dst buffer (len=%lu offset=%x)\n",
970 __func__, dst_len, info.dst.offset);
971 rc = -ERANGE;
972 goto do_rotate_unlock_mutex;
973 }
974
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700975 rc = get_img(&info.src_chroma, 1,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976 (unsigned long *)&in_chroma_paddr,
Kuogee Hsiehbeaa9352012-02-24 16:59:26 -0800977 (unsigned long *)&src_len, &srcp1_file, &p_need,
Naseer Ahmed18018602011-10-25 13:32:58 -0700978 &srcp1_ihdl);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700979 if (rc) {
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700980 pr_err("%s: in chroma get_img() failed id=0x%08x\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981 DRIVER_NAME, info.src_chroma.memory_id);
982 goto do_rotate_unlock_mutex;
983 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -0700985 rc = get_img(&info.dst_chroma, 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700986 (unsigned long *)&out_chroma_paddr,
Kuogee Hsiehbeaa9352012-02-24 16:59:26 -0800987 (unsigned long *)&dst_len, &dstp1_file, &p_need,
Naseer Ahmed18018602011-10-25 13:32:58 -0700988 &dstp1_ihdl);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700989 if (rc) {
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700990 pr_err("%s: out chroma get_img() failed id=0x%08x\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991 DRIVER_NAME, info.dst_chroma.memory_id);
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700992 goto do_rotate_unlock_mutex;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993 }
Adrian Salido-Moreno88411862011-09-20 18:54:03 -0700994
995 if (checkoffset(info.src_chroma.offset,
996 src_planes.plane_size[1],
997 src_len)) {
998 pr_err("%s: invalid chr src buf len=%lu offset=%x\n",
999 __func__, src_len, info.src_chroma.offset);
1000 rc = -ERANGE;
1001 goto do_rotate_unlock_mutex;
1002 }
1003
1004 if (checkoffset(info.dst_chroma.offset,
1005 src_planes.plane_size[1],
1006 dst_len)) {
1007 pr_err("%s: invalid chr dst buf len=%lu offset=%x\n",
1008 __func__, dst_len, info.dst_chroma.offset);
1009 rc = -ERANGE;
1010 goto do_rotate_unlock_mutex;
1011 }
1012
1013 in_chroma_paddr += info.src_chroma.offset;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014 out_chroma_paddr += info.dst_chroma.offset;
Adrian Salido-Moreno88411862011-09-20 18:54:03 -07001015 } else {
1016 if (checkoffset(info.src.offset,
1017 src_planes.total_size,
1018 src_len)) {
1019 pr_err("%s: invalid src buffer (len=%lu offset=%x)\n",
1020 __func__, src_len, info.src.offset);
1021 rc = -ERANGE;
1022 goto do_rotate_unlock_mutex;
1023 }
1024 if (checkoffset(info.dst.offset,
1025 dst_planes.total_size,
1026 dst_len)) {
1027 pr_err("%s: invalid dst buffer (len=%lu offset=%x)\n",
1028 __func__, dst_len, info.dst.offset);
1029 rc = -ERANGE;
1030 goto do_rotate_unlock_mutex;
1031 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001032 }
1033
Adrian Salido-Moreno88411862011-09-20 18:54:03 -07001034 in_paddr += info.src.offset;
1035 out_paddr += info.dst.offset;
1036
1037 if (!in_chroma_paddr && src_planes.num_planes >= 2)
1038 in_chroma_paddr = in_paddr + src_planes.plane_size[0];
1039 if (!out_chroma_paddr && dst_planes.num_planes >= 2)
1040 out_chroma_paddr = out_paddr + dst_planes.plane_size[0];
1041 if (src_planes.num_planes >= 3)
1042 in_chroma2_paddr = in_chroma_paddr + src_planes.plane_size[1];
1043
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001044 cancel_delayed_work(&msm_rotator_dev->rot_clk_work);
1045 if (msm_rotator_dev->rot_clk_state != CLK_EN) {
1046 enable_rot_clks();
1047 msm_rotator_dev->rot_clk_state = CLK_EN;
1048 }
1049 enable_irq(msm_rotator_dev->irq);
1050
1051#ifdef CONFIG_MSM_ROTATOR_USE_IMEM
1052 use_imem = msm_rotator_imem_allocate(ROTATOR_REQUEST);
1053#else
1054 use_imem = 0;
1055#endif
1056 /*
1057 * workaround for a hardware bug. rotator hardware hangs when we
1058 * use write burst beat size 16 on 128X128 tile fetch mode. As a
1059 * temporary fix use 0x42 for BURST_SIZE when imem used.
1060 */
1061 if (use_imem)
1062 iowrite32(0x42, MSM_ROTATOR_MAX_BURST_SIZE);
1063
1064 iowrite32(((msm_rotator_dev->img_info[s]->src_rect.h & 0x1fff)
1065 << 16) |
1066 (msm_rotator_dev->img_info[s]->src_rect.w & 0x1fff),
1067 MSM_ROTATOR_SRC_SIZE);
1068 iowrite32(((msm_rotator_dev->img_info[s]->src_rect.y & 0x1fff)
1069 << 16) |
1070 (msm_rotator_dev->img_info[s]->src_rect.x & 0x1fff),
1071 MSM_ROTATOR_SRC_XY);
1072 iowrite32(((msm_rotator_dev->img_info[s]->src.height & 0x1fff)
1073 << 16) |
1074 (msm_rotator_dev->img_info[s]->src.width & 0x1fff),
1075 MSM_ROTATOR_SRC_IMAGE_SIZE);
1076
1077 switch (format) {
1078 case MDP_RGB_565:
1079 case MDP_BGR_565:
1080 case MDP_RGB_888:
1081 case MDP_ARGB_8888:
1082 case MDP_RGBA_8888:
1083 case MDP_XRGB_8888:
1084 case MDP_BGRA_8888:
1085 case MDP_RGBX_8888:
1086 rc = msm_rotator_rgb_types(msm_rotator_dev->img_info[s],
1087 in_paddr, out_paddr,
1088 use_imem,
1089 msm_rotator_dev->last_session_idx
1090 != s);
1091 break;
1092 case MDP_Y_CBCR_H2V2:
1093 case MDP_Y_CRCB_H2V2:
Adrian Salido-Moreno0644f342011-07-08 12:05:01 -07001094 case MDP_Y_CB_CR_H2V2:
1095 case MDP_Y_CR_CB_H2V2:
Pradeep Jilagam9b4a6be2011-10-03 17:19:20 +05301096 case MDP_Y_CR_CB_GH2V2:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097 case MDP_Y_CRCB_H2V2_TILE:
1098 case MDP_Y_CBCR_H2V2_TILE:
Adrian Salido-Moreno88411862011-09-20 18:54:03 -07001099 rc = msm_rotator_ycxcx_h2v2(msm_rotator_dev->img_info[s],
1100 in_paddr, out_paddr, use_imem,
1101 msm_rotator_dev->last_session_idx
1102 != s,
1103 in_chroma_paddr,
1104 out_chroma_paddr,
1105 in_chroma2_paddr);
1106 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001107 case MDP_Y_CBCR_H2V1:
1108 case MDP_Y_CRCB_H2V1:
1109 rc = msm_rotator_ycxcx_h2v1(msm_rotator_dev->img_info[s],
1110 in_paddr, out_paddr, use_imem,
1111 msm_rotator_dev->last_session_idx
1112 != s,
1113 in_chroma_paddr,
1114 out_chroma_paddr);
1115 break;
1116 case MDP_YCRYCB_H2V1:
1117 rc = msm_rotator_ycrycb(msm_rotator_dev->img_info[s],
1118 in_paddr, out_paddr, use_imem,
Mayank Chopra732dcd62012-01-09 20:53:39 +05301119 msm_rotator_dev->last_session_idx != s,
1120 out_chroma_paddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121 break;
1122 default:
1123 rc = -EINVAL;
1124 goto do_rotate_exit;
1125 }
1126
1127 if (rc != 0) {
1128 msm_rotator_dev->last_session_idx = INVALID_SESSION;
1129 goto do_rotate_exit;
1130 }
1131
1132 iowrite32(3, MSM_ROTATOR_INTR_ENABLE);
1133
1134 msm_rotator_dev->processing = 1;
1135 iowrite32(0x1, MSM_ROTATOR_START);
1136
1137 wait_event(msm_rotator_dev->wq,
1138 (msm_rotator_dev->processing == 0));
1139 status = (unsigned char)ioread32(MSM_ROTATOR_INTR_STATUS);
1140 if ((status & 0x03) != 0x01)
1141 rc = -EFAULT;
1142 iowrite32(0, MSM_ROTATOR_INTR_ENABLE);
1143 iowrite32(3, MSM_ROTATOR_INTR_CLEAR);
1144
1145do_rotate_exit:
1146 disable_irq(msm_rotator_dev->irq);
1147#ifdef CONFIG_MSM_ROTATOR_USE_IMEM
1148 msm_rotator_imem_free(ROTATOR_REQUEST);
1149#endif
1150 schedule_delayed_work(&msm_rotator_dev->rot_clk_work, HZ);
Adrian Salido-Moreno88411862011-09-20 18:54:03 -07001151do_rotate_unlock_mutex:
Naseer Ahmed18018602011-10-25 13:32:58 -07001152 put_img(dstp1_file, dstp1_ihdl);
1153 put_img(srcp1_file, srcp1_ihdl);
1154 put_img(dstp0_file, dstp0_ihdl);
Kuogee Hsiehbeaa9352012-02-24 16:59:26 -08001155
1156 /* only source may use frame buffer */
1157 if (info.src.flags & MDP_MEMORY_ID_TYPE_FB)
1158 fput_light(srcp0_file, ps0_need);
1159 else
1160 put_img(srcp0_file, srcp0_ihdl);
Adrian Salido-Moreno88411862011-09-20 18:54:03 -07001161 mutex_unlock(&msm_rotator_dev->rotator_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001162 dev_dbg(msm_rotator_dev->device, "%s() returning rc = %d\n",
1163 __func__, rc);
1164 return rc;
1165}
1166
Nagamalleswararao Ganjie1a9f872011-11-06 23:13:32 -08001167static void msm_rotator_set_perf_level(u32 wh, u32 is_rgb)
1168{
1169 u32 perf_level;
1170
1171 if (is_rgb)
1172 perf_level = 1;
1173 else if (wh <= (640 * 480))
1174 perf_level = 2;
1175 else if (wh <= (736 * 1280))
1176 perf_level = 3;
1177 else
1178 perf_level = 4;
1179
1180#ifdef CONFIG_MSM_BUS_SCALING
1181 msm_bus_scale_client_update_request(msm_rotator_dev->bus_client_handle,
1182 perf_level);
1183#endif
1184
1185}
1186
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001187static int msm_rotator_start(unsigned long arg,
1188 struct msm_rotator_fd_info *fd_info)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189{
1190 struct msm_rotator_img_info info;
1191 int rc = 0;
Nagamalleswararao Ganjie1a9f872011-11-06 23:13:32 -08001192 int s, is_rgb = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001193 int first_free_index = INVALID_SESSION;
Adrian Salido-Moreno88411862011-09-20 18:54:03 -07001194 unsigned int dst_w, dst_h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001195
1196 if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
1197 return -EFAULT;
1198
1199 if ((info.rotations > MSM_ROTATOR_MAX_ROT) ||
1200 (info.src.height > MSM_ROTATOR_MAX_H) ||
1201 (info.src.width > MSM_ROTATOR_MAX_W) ||
1202 (info.dst.height > MSM_ROTATOR_MAX_H) ||
1203 (info.dst.width > MSM_ROTATOR_MAX_W) ||
Adrian Salido-Moreno67273e52011-10-21 19:04:18 -07001204 (info.downscale_ratio > MAX_DOWNSCALE_RATIO)) {
1205 pr_err("%s: Invalid parameters\n", __func__);
1206 return -EINVAL;
1207 }
1208
1209 if (info.rotations & MDP_ROT_90) {
1210 dst_w = info.src_rect.h >> info.downscale_ratio;
1211 dst_h = info.src_rect.w >> info.downscale_ratio;
1212 } else {
1213 dst_w = info.src_rect.w >> info.downscale_ratio;
1214 dst_h = info.src_rect.h >> info.downscale_ratio;
1215 }
1216
1217 if (checkoffset(info.src_rect.x, info.src_rect.w, info.src.width) ||
Adrian Salido-Moreno88411862011-09-20 18:54:03 -07001218 checkoffset(info.src_rect.y, info.src_rect.h, info.src.height) ||
1219 checkoffset(info.dst_x, dst_w, info.dst.width) ||
Adrian Salido-Moreno67273e52011-10-21 19:04:18 -07001220 checkoffset(info.dst_y, dst_h, info.dst.height)) {
1221 pr_err("%s: Invalid src or dst rect\n", __func__);
1222 return -ERANGE;
1223 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001224
1225 switch (info.src.format) {
1226 case MDP_RGB_565:
1227 case MDP_BGR_565:
1228 case MDP_RGB_888:
1229 case MDP_ARGB_8888:
1230 case MDP_RGBA_8888:
1231 case MDP_XRGB_8888:
1232 case MDP_RGBX_8888:
1233 case MDP_BGRA_8888:
Nagamalleswararao Ganjie1a9f872011-11-06 23:13:32 -08001234 is_rgb = 1;
Mayank Chopra012a8e72012-04-11 10:41:13 +05301235 info.dst.format = info.src.format;
Nagamalleswararao Ganjie1a9f872011-11-06 23:13:32 -08001236 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001237 case MDP_Y_CBCR_H2V2:
1238 case MDP_Y_CRCB_H2V2:
Mayank Chopra012a8e72012-04-11 10:41:13 +05301239 case MDP_Y_CBCR_H2V1:
1240 case MDP_Y_CRCB_H2V1:
1241 info.dst.format = info.src.format;
1242 break;
1243 case MDP_YCRYCB_H2V1:
1244 info.dst.format = MDP_Y_CRCB_H2V1;
1245 break;
Adrian Salido-Moreno0644f342011-07-08 12:05:01 -07001246 case MDP_Y_CB_CR_H2V2:
Mayank Chopra012a8e72012-04-11 10:41:13 +05301247 case MDP_Y_CBCR_H2V2_TILE:
1248 info.dst.format = MDP_Y_CBCR_H2V2;
1249 break;
Adrian Salido-Moreno0644f342011-07-08 12:05:01 -07001250 case MDP_Y_CR_CB_H2V2:
Pradeep Jilagam9b4a6be2011-10-03 17:19:20 +05301251 case MDP_Y_CR_CB_GH2V2:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001252 case MDP_Y_CRCB_H2V2_TILE:
Mayank Chopra012a8e72012-04-11 10:41:13 +05301253 info.dst.format = MDP_Y_CRCB_H2V2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001254 break;
1255 default:
1256 return -EINVAL;
1257 }
1258
1259 mutex_lock(&msm_rotator_dev->rotator_lock);
Nagamalleswararao Ganjie1a9f872011-11-06 23:13:32 -08001260
1261 msm_rotator_set_perf_level((info.src.width*info.src.height), is_rgb);
1262
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001263 for (s = 0; s < MAX_SESSIONS; s++) {
1264 if ((msm_rotator_dev->img_info[s] != NULL) &&
1265 (info.session_id ==
1266 (unsigned int)msm_rotator_dev->img_info[s]
1267 )) {
1268 *(msm_rotator_dev->img_info[s]) = info;
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001269 msm_rotator_dev->fd_info[s] = fd_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001270
1271 if (msm_rotator_dev->last_session_idx == s)
1272 msm_rotator_dev->last_session_idx =
1273 INVALID_SESSION;
1274 break;
1275 }
1276
1277 if ((msm_rotator_dev->img_info[s] == NULL) &&
1278 (first_free_index ==
1279 INVALID_SESSION))
1280 first_free_index = s;
1281 }
1282
1283 if ((s == MAX_SESSIONS) && (first_free_index != INVALID_SESSION)) {
1284 /* allocate a session id */
1285 msm_rotator_dev->img_info[first_free_index] =
1286 kzalloc(sizeof(struct msm_rotator_img_info),
1287 GFP_KERNEL);
1288 if (!msm_rotator_dev->img_info[first_free_index]) {
1289 printk(KERN_ERR "%s : unable to alloc mem\n",
1290 __func__);
1291 rc = -ENOMEM;
1292 goto rotator_start_exit;
1293 }
1294 info.session_id = (unsigned int)
1295 msm_rotator_dev->img_info[first_free_index];
1296 *(msm_rotator_dev->img_info[first_free_index]) = info;
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001297 msm_rotator_dev->fd_info[first_free_index] = fd_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001298
1299 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
1300 rc = -EFAULT;
1301 } else if (s == MAX_SESSIONS) {
1302 dev_dbg(msm_rotator_dev->device, "%s: all sessions in use\n",
1303 __func__);
1304 rc = -EBUSY;
1305 }
1306
1307rotator_start_exit:
1308 mutex_unlock(&msm_rotator_dev->rotator_lock);
1309
1310 return rc;
1311}
1312
1313static int msm_rotator_finish(unsigned long arg)
1314{
1315 int rc = 0;
1316 int s;
1317 unsigned int session_id;
1318
1319 if (copy_from_user(&session_id, (void __user *)arg, sizeof(s)))
1320 return -EFAULT;
1321
1322 mutex_lock(&msm_rotator_dev->rotator_lock);
1323 for (s = 0; s < MAX_SESSIONS; s++) {
1324 if ((msm_rotator_dev->img_info[s] != NULL) &&
1325 (session_id ==
1326 (unsigned int)msm_rotator_dev->img_info[s])) {
1327 if (msm_rotator_dev->last_session_idx == s)
1328 msm_rotator_dev->last_session_idx =
1329 INVALID_SESSION;
1330 kfree(msm_rotator_dev->img_info[s]);
1331 msm_rotator_dev->img_info[s] = NULL;
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001332 msm_rotator_dev->fd_info[s] = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001333 break;
1334 }
1335 }
1336
1337 if (s == MAX_SESSIONS)
1338 rc = -EINVAL;
Nagamalleswararao Ganjie1a9f872011-11-06 23:13:32 -08001339#ifdef CONFIG_MSM_BUS_SCALING
1340 msm_bus_scale_client_update_request(msm_rotator_dev->bus_client_handle,
1341 0);
1342#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001343 mutex_unlock(&msm_rotator_dev->rotator_lock);
1344 return rc;
1345}
1346
1347static int
1348msm_rotator_open(struct inode *inode, struct file *filp)
1349{
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001350 struct msm_rotator_fd_info *tmp, *fd_info = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001351 int i;
1352
1353 if (filp->private_data)
1354 return -EBUSY;
1355
1356 mutex_lock(&msm_rotator_dev->rotator_lock);
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001357 for (i = 0; i < MAX_SESSIONS; i++) {
1358 if (msm_rotator_dev->fd_info[i] == NULL)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001359 break;
1360 }
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001361
1362 if (i == MAX_SESSIONS) {
1363 mutex_unlock(&msm_rotator_dev->rotator_lock);
1364 return -EBUSY;
1365 }
1366
1367 list_for_each_entry(tmp, &msm_rotator_dev->fd_list, list) {
1368 if (tmp->pid == current->pid) {
1369 fd_info = tmp;
1370 break;
1371 }
1372 }
1373
1374 if (!fd_info) {
1375 fd_info = kzalloc(sizeof(*fd_info), GFP_KERNEL);
1376 if (!fd_info) {
1377 mutex_unlock(&msm_rotator_dev->rotator_lock);
1378 pr_err("%s: insufficient memory to alloc resources\n",
1379 __func__);
1380 return -ENOMEM;
1381 }
1382 list_add(&fd_info->list, &msm_rotator_dev->fd_list);
1383 fd_info->pid = current->pid;
1384 }
1385 fd_info->ref_cnt++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001386 mutex_unlock(&msm_rotator_dev->rotator_lock);
1387
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001388 filp->private_data = fd_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001389
1390 return 0;
1391}
1392
1393static int
1394msm_rotator_close(struct inode *inode, struct file *filp)
1395{
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001396 struct msm_rotator_fd_info *fd_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001397 int s;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001398
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001399 fd_info = (struct msm_rotator_fd_info *)filp->private_data;
1400
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001401 mutex_lock(&msm_rotator_dev->rotator_lock);
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001402 if (--fd_info->ref_cnt > 0) {
1403 mutex_unlock(&msm_rotator_dev->rotator_lock);
1404 return 0;
1405 }
1406
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001407 for (s = 0; s < MAX_SESSIONS; s++) {
1408 if (msm_rotator_dev->img_info[s] != NULL &&
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001409 msm_rotator_dev->fd_info[s] == fd_info) {
1410 pr_debug("%s: freeing rotator session %p (pid %d)\n",
1411 __func__, msm_rotator_dev->img_info[s],
1412 fd_info->pid);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001413 kfree(msm_rotator_dev->img_info[s]);
1414 msm_rotator_dev->img_info[s] = NULL;
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001415 msm_rotator_dev->fd_info[s] = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001416 if (msm_rotator_dev->last_session_idx == s)
1417 msm_rotator_dev->last_session_idx =
1418 INVALID_SESSION;
1419 }
1420 }
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001421 list_del(&fd_info->list);
1422 kfree(fd_info);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001423 mutex_unlock(&msm_rotator_dev->rotator_lock);
1424
1425 return 0;
1426}
1427
1428static long msm_rotator_ioctl(struct file *file, unsigned cmd,
1429 unsigned long arg)
1430{
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001431 struct msm_rotator_fd_info *fd_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001432
1433 if (_IOC_TYPE(cmd) != MSM_ROTATOR_IOCTL_MAGIC)
1434 return -ENOTTY;
1435
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001436 fd_info = (struct msm_rotator_fd_info *)file->private_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001437
1438 switch (cmd) {
1439 case MSM_ROTATOR_IOCTL_START:
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001440 return msm_rotator_start(arg, fd_info);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001441 case MSM_ROTATOR_IOCTL_ROTATE:
1442 return msm_rotator_do_rotate(arg);
1443 case MSM_ROTATOR_IOCTL_FINISH:
1444 return msm_rotator_finish(arg);
1445
1446 default:
1447 dev_dbg(msm_rotator_dev->device,
1448 "unexpected IOCTL %d\n", cmd);
1449 return -ENOTTY;
1450 }
1451}
1452
1453static const struct file_operations msm_rotator_fops = {
1454 .owner = THIS_MODULE,
1455 .open = msm_rotator_open,
1456 .release = msm_rotator_close,
1457 .unlocked_ioctl = msm_rotator_ioctl,
1458};
1459
1460static int __devinit msm_rotator_probe(struct platform_device *pdev)
1461{
1462 int rc = 0;
1463 struct resource *res;
1464 struct msm_rotator_platform_data *pdata = NULL;
1465 int i, number_of_clks;
1466 uint32_t ver;
1467
1468 msm_rotator_dev = kzalloc(sizeof(struct msm_rotator_dev), GFP_KERNEL);
1469 if (!msm_rotator_dev) {
1470 printk(KERN_ERR "%s Unable to allocate memory for struct\n",
1471 __func__);
1472 return -ENOMEM;
1473 }
1474 for (i = 0; i < MAX_SESSIONS; i++)
1475 msm_rotator_dev->img_info[i] = NULL;
1476 msm_rotator_dev->last_session_idx = INVALID_SESSION;
1477
1478 pdata = pdev->dev.platform_data;
1479 number_of_clks = pdata->number_of_clocks;
1480
1481 msm_rotator_dev->imem_owner = IMEM_NO_OWNER;
1482 mutex_init(&msm_rotator_dev->imem_lock);
Adrian Salido-Moreno5737db12012-04-02 15:22:08 -07001483 INIT_LIST_HEAD(&msm_rotator_dev->fd_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001484 msm_rotator_dev->imem_clk_state = CLK_DIS;
1485 INIT_DELAYED_WORK(&msm_rotator_dev->imem_clk_work,
1486 msm_rotator_imem_clk_work_f);
1487 msm_rotator_dev->imem_clk = NULL;
1488 msm_rotator_dev->pdev = pdev;
1489
1490 msm_rotator_dev->core_clk = NULL;
1491 msm_rotator_dev->pclk = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001492
Nagamalleswararao Ganjie1a9f872011-11-06 23:13:32 -08001493#ifdef CONFIG_MSM_BUS_SCALING
1494 if (!msm_rotator_dev->bus_client_handle && pdata &&
1495 pdata->bus_scale_table) {
1496 msm_rotator_dev->bus_client_handle =
1497 msm_bus_scale_register_client(
1498 pdata->bus_scale_table);
1499 if (!msm_rotator_dev->bus_client_handle) {
1500 pr_err("%s not able to get bus scale handle\n",
1501 __func__);
1502 }
1503 }
1504#endif
1505
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001506 for (i = 0; i < number_of_clks; i++) {
1507 if (pdata->rotator_clks[i].clk_type == ROTATOR_IMEM_CLK) {
1508 msm_rotator_dev->imem_clk =
1509 clk_get(&msm_rotator_dev->pdev->dev,
1510 pdata->rotator_clks[i].clk_name);
1511 if (IS_ERR(msm_rotator_dev->imem_clk)) {
1512 rc = PTR_ERR(msm_rotator_dev->imem_clk);
1513 msm_rotator_dev->imem_clk = NULL;
1514 printk(KERN_ERR "%s: cannot get imem_clk "
1515 "rc=%d\n", DRIVER_NAME, rc);
1516 goto error_imem_clk;
1517 }
1518 if (pdata->rotator_clks[i].clk_rate)
Matt Wagantall754f2472011-11-08 15:44:00 -08001519 clk_set_rate(msm_rotator_dev->imem_clk,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001520 pdata->rotator_clks[i].clk_rate);
1521 }
1522 if (pdata->rotator_clks[i].clk_type == ROTATOR_PCLK) {
1523 msm_rotator_dev->pclk =
1524 clk_get(&msm_rotator_dev->pdev->dev,
1525 pdata->rotator_clks[i].clk_name);
1526 if (IS_ERR(msm_rotator_dev->pclk)) {
1527 rc = PTR_ERR(msm_rotator_dev->pclk);
1528 msm_rotator_dev->pclk = NULL;
1529 printk(KERN_ERR "%s: cannot get pclk rc=%d\n",
1530 DRIVER_NAME, rc);
1531 goto error_pclk;
1532 }
1533
1534 if (pdata->rotator_clks[i].clk_rate)
Matt Wagantall754f2472011-11-08 15:44:00 -08001535 clk_set_rate(msm_rotator_dev->pclk,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001536 pdata->rotator_clks[i].clk_rate);
1537 }
1538
1539 if (pdata->rotator_clks[i].clk_type == ROTATOR_CORE_CLK) {
1540 msm_rotator_dev->core_clk =
1541 clk_get(&msm_rotator_dev->pdev->dev,
1542 pdata->rotator_clks[i].clk_name);
1543 if (IS_ERR(msm_rotator_dev->core_clk)) {
1544 rc = PTR_ERR(msm_rotator_dev->core_clk);
1545 msm_rotator_dev->core_clk = NULL;
1546 printk(KERN_ERR "%s: cannot get core clk "
1547 "rc=%d\n", DRIVER_NAME, rc);
1548 goto error_core_clk;
1549 }
1550
1551 if (pdata->rotator_clks[i].clk_rate)
Matt Wagantall754f2472011-11-08 15:44:00 -08001552 clk_set_rate(msm_rotator_dev->core_clk,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001553 pdata->rotator_clks[i].clk_rate);
1554 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001555 }
1556
1557 msm_rotator_dev->regulator = regulator_get(NULL, pdata->regulator_name);
1558 if (IS_ERR(msm_rotator_dev->regulator))
1559 msm_rotator_dev->regulator = NULL;
1560
1561 msm_rotator_dev->rot_clk_state = CLK_DIS;
1562 INIT_DELAYED_WORK(&msm_rotator_dev->rot_clk_work,
1563 msm_rotator_rot_clk_work_f);
1564
1565 mutex_init(&msm_rotator_dev->rotator_lock);
Naseer Ahmed18018602011-10-25 13:32:58 -07001566#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
1567 msm_rotator_dev->client = msm_ion_client_create(-1, pdev->name);
1568#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001569 platform_set_drvdata(pdev, msm_rotator_dev);
1570
1571
1572 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1573 if (!res) {
1574 printk(KERN_ALERT
1575 "%s: could not get IORESOURCE_MEM\n", DRIVER_NAME);
1576 rc = -ENODEV;
1577 goto error_get_resource;
1578 }
1579 msm_rotator_dev->io_base = ioremap(res->start,
1580 resource_size(res));
1581
1582#ifdef CONFIG_MSM_ROTATOR_USE_IMEM
1583 if (msm_rotator_dev->imem_clk)
Ravishangar Kalyanamd16485d2012-03-21 16:51:26 -07001584 clk_prepare_enable(msm_rotator_dev->imem_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001585#endif
1586 enable_rot_clks();
1587 ver = ioread32(MSM_ROTATOR_HW_VERSION);
1588 disable_rot_clks();
1589
1590#ifdef CONFIG_MSM_ROTATOR_USE_IMEM
1591 if (msm_rotator_dev->imem_clk)
Ravishangar Kalyanamd16485d2012-03-21 16:51:26 -07001592 clk_disable_unprepare(msm_rotator_dev->imem_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001593#endif
Naseer Ahmed18018602011-10-25 13:32:58 -07001594 if (ver != pdata->hardware_version_number)
Mayank Chopra012a8e72012-04-11 10:41:13 +05301595 pr_debug("%s: invalid HW version ver 0x%x\n",
Ravishangar Kalyanam6bc448a2012-03-14 11:31:52 -07001596 DRIVER_NAME, ver);
Naseer Ahmed18018602011-10-25 13:32:58 -07001597
Mayank Chopra012a8e72012-04-11 10:41:13 +05301598 rotator_hw_revision = ver;
1599 rotator_hw_revision >>= 16; /* bit 31:16 */
1600 rotator_hw_revision &= 0xff;
1601
1602 pr_info("%s: rotator_hw_revision=%x\n",
1603 __func__, rotator_hw_revision);
1604
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001605 msm_rotator_dev->irq = platform_get_irq(pdev, 0);
1606 if (msm_rotator_dev->irq < 0) {
1607 printk(KERN_ALERT "%s: could not get IORESOURCE_IRQ\n",
1608 DRIVER_NAME);
1609 rc = -ENODEV;
1610 goto error_get_irq;
1611 }
1612 rc = request_irq(msm_rotator_dev->irq, msm_rotator_isr,
1613 IRQF_TRIGGER_RISING, DRIVER_NAME, NULL);
1614 if (rc) {
1615 printk(KERN_ERR "%s: request_irq() failed\n", DRIVER_NAME);
1616 goto error_get_irq;
1617 }
1618 /* we enable the IRQ when we need it in the ioctl */
1619 disable_irq(msm_rotator_dev->irq);
1620
1621 rc = alloc_chrdev_region(&msm_rotator_dev->dev_num, 0, 1, DRIVER_NAME);
1622 if (rc < 0) {
1623 printk(KERN_ERR "%s: alloc_chrdev_region Failed rc = %d\n",
1624 __func__, rc);
1625 goto error_get_irq;
1626 }
1627
1628 msm_rotator_dev->class = class_create(THIS_MODULE, DRIVER_NAME);
1629 if (IS_ERR(msm_rotator_dev->class)) {
1630 rc = PTR_ERR(msm_rotator_dev->class);
1631 printk(KERN_ERR "%s: couldn't create class rc = %d\n",
1632 DRIVER_NAME, rc);
1633 goto error_class_create;
1634 }
1635
1636 msm_rotator_dev->device = device_create(msm_rotator_dev->class, NULL,
1637 msm_rotator_dev->dev_num, NULL,
1638 DRIVER_NAME);
1639 if (IS_ERR(msm_rotator_dev->device)) {
1640 rc = PTR_ERR(msm_rotator_dev->device);
1641 printk(KERN_ERR "%s: device_create failed %d\n",
1642 DRIVER_NAME, rc);
1643 goto error_class_device_create;
1644 }
1645
1646 cdev_init(&msm_rotator_dev->cdev, &msm_rotator_fops);
1647 rc = cdev_add(&msm_rotator_dev->cdev,
1648 MKDEV(MAJOR(msm_rotator_dev->dev_num), 0),
1649 1);
1650 if (rc < 0) {
1651 printk(KERN_ERR "%s: cdev_add failed %d\n", __func__, rc);
1652 goto error_cdev_add;
1653 }
1654
1655 init_waitqueue_head(&msm_rotator_dev->wq);
1656
1657 dev_dbg(msm_rotator_dev->device, "probe successful\n");
1658 return rc;
1659
1660error_cdev_add:
1661 device_destroy(msm_rotator_dev->class, msm_rotator_dev->dev_num);
1662error_class_device_create:
1663 class_destroy(msm_rotator_dev->class);
1664error_class_create:
1665 unregister_chrdev_region(msm_rotator_dev->dev_num, 1);
1666error_get_irq:
1667 iounmap(msm_rotator_dev->io_base);
1668error_get_resource:
1669 mutex_destroy(&msm_rotator_dev->rotator_lock);
1670 if (msm_rotator_dev->regulator)
1671 regulator_put(msm_rotator_dev->regulator);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001672 clk_put(msm_rotator_dev->core_clk);
1673error_core_clk:
1674 clk_put(msm_rotator_dev->pclk);
1675error_pclk:
1676 if (msm_rotator_dev->imem_clk)
1677 clk_put(msm_rotator_dev->imem_clk);
1678error_imem_clk:
1679 mutex_destroy(&msm_rotator_dev->imem_lock);
1680 kfree(msm_rotator_dev);
1681 return rc;
1682}
1683
1684static int __devexit msm_rotator_remove(struct platform_device *plat_dev)
1685{
1686 int i;
1687
Nagamalleswararao Ganjie1a9f872011-11-06 23:13:32 -08001688#ifdef CONFIG_MSM_BUS_SCALING
1689 msm_bus_scale_unregister_client(msm_rotator_dev->bus_client_handle);
1690#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001691 free_irq(msm_rotator_dev->irq, NULL);
1692 mutex_destroy(&msm_rotator_dev->rotator_lock);
1693 cdev_del(&msm_rotator_dev->cdev);
1694 device_destroy(msm_rotator_dev->class, msm_rotator_dev->dev_num);
1695 class_destroy(msm_rotator_dev->class);
1696 unregister_chrdev_region(msm_rotator_dev->dev_num, 1);
1697 iounmap(msm_rotator_dev->io_base);
1698 if (msm_rotator_dev->imem_clk) {
1699 if (msm_rotator_dev->imem_clk_state == CLK_EN)
Ravishangar Kalyanamd16485d2012-03-21 16:51:26 -07001700 clk_disable_unprepare(msm_rotator_dev->imem_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001701 clk_put(msm_rotator_dev->imem_clk);
1702 msm_rotator_dev->imem_clk = NULL;
1703 }
1704 if (msm_rotator_dev->rot_clk_state == CLK_EN)
1705 disable_rot_clks();
1706 clk_put(msm_rotator_dev->core_clk);
1707 clk_put(msm_rotator_dev->pclk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001708 if (msm_rotator_dev->regulator)
1709 regulator_put(msm_rotator_dev->regulator);
1710 msm_rotator_dev->core_clk = NULL;
1711 msm_rotator_dev->pclk = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001712 mutex_destroy(&msm_rotator_dev->imem_lock);
1713 for (i = 0; i < MAX_SESSIONS; i++)
1714 if (msm_rotator_dev->img_info[i] != NULL)
1715 kfree(msm_rotator_dev->img_info[i]);
1716 kfree(msm_rotator_dev);
1717 return 0;
1718}
1719
1720#ifdef CONFIG_PM
1721static int msm_rotator_suspend(struct platform_device *dev, pm_message_t state)
1722{
1723 mutex_lock(&msm_rotator_dev->imem_lock);
1724 if (msm_rotator_dev->imem_clk_state == CLK_EN
1725 && msm_rotator_dev->imem_clk) {
Ravishangar Kalyanamd16485d2012-03-21 16:51:26 -07001726 clk_disable_unprepare(msm_rotator_dev->imem_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001727 msm_rotator_dev->imem_clk_state = CLK_SUSPEND;
1728 }
1729 mutex_unlock(&msm_rotator_dev->imem_lock);
1730 mutex_lock(&msm_rotator_dev->rotator_lock);
1731 if (msm_rotator_dev->rot_clk_state == CLK_EN) {
1732 disable_rot_clks();
1733 msm_rotator_dev->rot_clk_state = CLK_SUSPEND;
1734 }
1735 mutex_unlock(&msm_rotator_dev->rotator_lock);
1736 return 0;
1737}
1738
1739static int msm_rotator_resume(struct platform_device *dev)
1740{
1741 mutex_lock(&msm_rotator_dev->imem_lock);
1742 if (msm_rotator_dev->imem_clk_state == CLK_SUSPEND
1743 && msm_rotator_dev->imem_clk) {
Ravishangar Kalyanamd16485d2012-03-21 16:51:26 -07001744 clk_prepare_enable(msm_rotator_dev->imem_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001745 msm_rotator_dev->imem_clk_state = CLK_EN;
1746 }
1747 mutex_unlock(&msm_rotator_dev->imem_lock);
1748 mutex_lock(&msm_rotator_dev->rotator_lock);
1749 if (msm_rotator_dev->rot_clk_state == CLK_SUSPEND) {
1750 enable_rot_clks();
1751 msm_rotator_dev->rot_clk_state = CLK_EN;
1752 }
1753 mutex_unlock(&msm_rotator_dev->rotator_lock);
1754 return 0;
1755}
1756#endif
1757
1758static struct platform_driver msm_rotator_platform_driver = {
1759 .probe = msm_rotator_probe,
1760 .remove = __devexit_p(msm_rotator_remove),
1761#ifdef CONFIG_PM
1762 .suspend = msm_rotator_suspend,
1763 .resume = msm_rotator_resume,
1764#endif
1765 .driver = {
1766 .owner = THIS_MODULE,
1767 .name = DRIVER_NAME
1768 }
1769};
1770
1771static int __init msm_rotator_init(void)
1772{
1773 return platform_driver_register(&msm_rotator_platform_driver);
1774}
1775
1776static void __exit msm_rotator_exit(void)
1777{
1778 return platform_driver_unregister(&msm_rotator_platform_driver);
1779}
1780
1781module_init(msm_rotator_init);
1782module_exit(msm_rotator_exit);
1783
1784MODULE_DESCRIPTION("MSM Offline Image Rotator driver");
1785MODULE_VERSION("1.0");
1786MODULE_LICENSE("GPL v2");