mdp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /* drivers/video/msm_fb/mdp.c
  2. *
  3. * MSM MDP Interface (used by framebuffer core)
  4. *
  5. * Copyright (C) 2007 QUALCOMM Incorporated
  6. * Copyright (C) 2007 Google Incorporated
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/fb.h>
  19. #include <linux/msm_mdp.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/wait.h>
  22. #include <linux/clk.h>
  23. #include <linux/file.h>
  24. #include <linux/major.h>
  25. #include <linux/slab.h>
  26. #include <mach/msm_iomap.h>
  27. #include <mach/msm_fb.h>
  28. #include <linux/platform_device.h>
  29. #include "mdp_hw.h"
  30. struct class *mdp_class;
  31. #define MDP_CMD_DEBUG_ACCESS_BASE (0x10000)
  32. static uint16_t mdp_default_ccs[] = {
  33. 0x254, 0x000, 0x331, 0x254, 0xF38, 0xE61, 0x254, 0x409, 0x000,
  34. 0x010, 0x080, 0x080
  35. };
  36. static DECLARE_WAIT_QUEUE_HEAD(mdp_dma2_waitqueue);
  37. static DECLARE_WAIT_QUEUE_HEAD(mdp_ppp_waitqueue);
  38. static struct msmfb_callback *dma_callback;
  39. static struct clk *clk;
  40. static unsigned int mdp_irq_mask;
  41. static DEFINE_SPINLOCK(mdp_lock);
  42. DEFINE_MUTEX(mdp_mutex);
  43. static int enable_mdp_irq(struct mdp_info *mdp, uint32_t mask)
  44. {
  45. unsigned long irq_flags;
  46. int ret = 0;
  47. BUG_ON(!mask);
  48. spin_lock_irqsave(&mdp_lock, irq_flags);
  49. /* if the mask bits are already set return an error, this interrupt
  50. * is already enabled */
  51. if (mdp_irq_mask & mask) {
  52. printk(KERN_ERR "mdp irq already on already on %x %x\n",
  53. mdp_irq_mask, mask);
  54. ret = -1;
  55. }
  56. /* if the mdp irq is not already enabled enable it */
  57. if (!mdp_irq_mask) {
  58. if (clk)
  59. clk_enable(clk);
  60. enable_irq(mdp->irq);
  61. }
  62. /* update the irq mask to reflect the fact that the interrupt is
  63. * enabled */
  64. mdp_irq_mask |= mask;
  65. spin_unlock_irqrestore(&mdp_lock, irq_flags);
  66. return ret;
  67. }
  68. static int locked_disable_mdp_irq(struct mdp_info *mdp, uint32_t mask)
  69. {
  70. /* this interrupt is already disabled! */
  71. if (!(mdp_irq_mask & mask)) {
  72. printk(KERN_ERR "mdp irq already off %x %x\n",
  73. mdp_irq_mask, mask);
  74. return -1;
  75. }
  76. /* update the irq mask to reflect the fact that the interrupt is
  77. * disabled */
  78. mdp_irq_mask &= ~(mask);
  79. /* if no one is waiting on the interrupt, disable it */
  80. if (!mdp_irq_mask) {
  81. disable_irq_nosync(mdp->irq);
  82. if (clk)
  83. clk_disable(clk);
  84. }
  85. return 0;
  86. }
  87. static int disable_mdp_irq(struct mdp_info *mdp, uint32_t mask)
  88. {
  89. unsigned long irq_flags;
  90. int ret;
  91. spin_lock_irqsave(&mdp_lock, irq_flags);
  92. ret = locked_disable_mdp_irq(mdp, mask);
  93. spin_unlock_irqrestore(&mdp_lock, irq_flags);
  94. return ret;
  95. }
  96. static irqreturn_t mdp_isr(int irq, void *data)
  97. {
  98. uint32_t status;
  99. unsigned long irq_flags;
  100. struct mdp_info *mdp = data;
  101. spin_lock_irqsave(&mdp_lock, irq_flags);
  102. status = mdp_readl(mdp, MDP_INTR_STATUS);
  103. mdp_writel(mdp, status, MDP_INTR_CLEAR);
  104. status &= mdp_irq_mask;
  105. if (status & DL0_DMA2_TERM_DONE) {
  106. if (dma_callback) {
  107. dma_callback->func(dma_callback);
  108. dma_callback = NULL;
  109. }
  110. wake_up(&mdp_dma2_waitqueue);
  111. }
  112. if (status & DL0_ROI_DONE)
  113. wake_up(&mdp_ppp_waitqueue);
  114. if (status)
  115. locked_disable_mdp_irq(mdp, status);
  116. spin_unlock_irqrestore(&mdp_lock, irq_flags);
  117. return IRQ_HANDLED;
  118. }
  119. static uint32_t mdp_check_mask(uint32_t mask)
  120. {
  121. uint32_t ret;
  122. unsigned long irq_flags;
  123. spin_lock_irqsave(&mdp_lock, irq_flags);
  124. ret = mdp_irq_mask & mask;
  125. spin_unlock_irqrestore(&mdp_lock, irq_flags);
  126. return ret;
  127. }
  128. static int mdp_wait(struct mdp_info *mdp, uint32_t mask, wait_queue_head_t *wq)
  129. {
  130. int ret = 0;
  131. unsigned long irq_flags;
  132. wait_event_timeout(*wq, !mdp_check_mask(mask), HZ);
  133. spin_lock_irqsave(&mdp_lock, irq_flags);
  134. if (mdp_irq_mask & mask) {
  135. locked_disable_mdp_irq(mdp, mask);
  136. printk(KERN_WARNING "timeout waiting for mdp to complete %x\n",
  137. mask);
  138. ret = -ETIMEDOUT;
  139. }
  140. spin_unlock_irqrestore(&mdp_lock, irq_flags);
  141. return ret;
  142. }
  143. void mdp_dma_wait(struct mdp_device *mdp_dev)
  144. {
  145. #define MDP_MAX_TIMEOUTS 20
  146. static int timeout_count;
  147. struct mdp_info *mdp = container_of(mdp_dev, struct mdp_info, mdp_dev);
  148. if (mdp_wait(mdp, DL0_DMA2_TERM_DONE, &mdp_dma2_waitqueue) == -ETIMEDOUT)
  149. timeout_count++;
  150. else
  151. timeout_count = 0;
  152. if (timeout_count > MDP_MAX_TIMEOUTS) {
  153. printk(KERN_ERR "mdp: dma failed %d times, somethings wrong!\n",
  154. MDP_MAX_TIMEOUTS);
  155. BUG();
  156. }
  157. }
  158. static int mdp_ppp_wait(struct mdp_info *mdp)
  159. {
  160. return mdp_wait(mdp, DL0_ROI_DONE, &mdp_ppp_waitqueue);
  161. }
  162. void mdp_dma_to_mddi(struct mdp_info *mdp, uint32_t addr, uint32_t stride,
  163. uint32_t width, uint32_t height, uint32_t x, uint32_t y,
  164. struct msmfb_callback *callback)
  165. {
  166. uint32_t dma2_cfg;
  167. uint16_t ld_param = 0; /* 0=PRIM, 1=SECD, 2=EXT */
  168. if (enable_mdp_irq(mdp, DL0_DMA2_TERM_DONE)) {
  169. printk(KERN_ERR "mdp_dma_to_mddi: busy\n");
  170. return;
  171. }
  172. dma_callback = callback;
  173. dma2_cfg = DMA_PACK_TIGHT |
  174. DMA_PACK_ALIGN_LSB |
  175. DMA_PACK_PATTERN_RGB |
  176. DMA_OUT_SEL_AHB |
  177. DMA_IBUF_NONCONTIGUOUS;
  178. dma2_cfg |= DMA_IBUF_FORMAT_RGB565;
  179. dma2_cfg |= DMA_OUT_SEL_MDDI;
  180. dma2_cfg |= DMA_MDDI_DMAOUT_LCD_SEL_PRIMARY;
  181. dma2_cfg |= DMA_DITHER_EN;
  182. /* setup size, address, and stride */
  183. mdp_writel(mdp, (height << 16) | (width),
  184. MDP_CMD_DEBUG_ACCESS_BASE + 0x0184);
  185. mdp_writel(mdp, addr, MDP_CMD_DEBUG_ACCESS_BASE + 0x0188);
  186. mdp_writel(mdp, stride, MDP_CMD_DEBUG_ACCESS_BASE + 0x018C);
  187. /* 666 18BPP */
  188. dma2_cfg |= DMA_DSTC0G_6BITS | DMA_DSTC1B_6BITS | DMA_DSTC2R_6BITS;
  189. /* set y & x offset and MDDI transaction parameters */
  190. mdp_writel(mdp, (y << 16) | (x), MDP_CMD_DEBUG_ACCESS_BASE + 0x0194);
  191. mdp_writel(mdp, ld_param, MDP_CMD_DEBUG_ACCESS_BASE + 0x01a0);
  192. mdp_writel(mdp, (MDDI_VDO_PACKET_DESC << 16) | MDDI_VDO_PACKET_PRIM,
  193. MDP_CMD_DEBUG_ACCESS_BASE + 0x01a4);
  194. mdp_writel(mdp, dma2_cfg, MDP_CMD_DEBUG_ACCESS_BASE + 0x0180);
  195. /* start DMA2 */
  196. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x0044);
  197. }
  198. void mdp_dma(struct mdp_device *mdp_dev, uint32_t addr, uint32_t stride,
  199. uint32_t width, uint32_t height, uint32_t x, uint32_t y,
  200. struct msmfb_callback *callback, int interface)
  201. {
  202. struct mdp_info *mdp = container_of(mdp_dev, struct mdp_info, mdp_dev);
  203. if (interface == MSM_MDDI_PMDH_INTERFACE) {
  204. mdp_dma_to_mddi(mdp, addr, stride, width, height, x, y,
  205. callback);
  206. }
  207. }
  208. int get_img(struct mdp_img *img, struct fb_info *info,
  209. unsigned long *start, unsigned long *len,
  210. struct file **filep)
  211. {
  212. int put_needed, ret = 0;
  213. struct file *file;
  214. file = fget_light(img->memory_id, &put_needed);
  215. if (file == NULL)
  216. return -1;
  217. if (MAJOR(file->f_dentry->d_inode->i_rdev) == FB_MAJOR) {
  218. *start = info->fix.smem_start;
  219. *len = info->fix.smem_len;
  220. } else
  221. ret = -1;
  222. fput_light(file, put_needed);
  223. return ret;
  224. }
  225. void put_img(struct file *src_file, struct file *dst_file)
  226. {
  227. }
  228. int mdp_blit(struct mdp_device *mdp_dev, struct fb_info *fb,
  229. struct mdp_blit_req *req)
  230. {
  231. int ret;
  232. unsigned long src_start = 0, src_len = 0, dst_start = 0, dst_len = 0;
  233. struct mdp_info *mdp = container_of(mdp_dev, struct mdp_info, mdp_dev);
  234. struct file *src_file = 0, *dst_file = 0;
  235. /* WORKAROUND FOR HARDWARE BUG IN BG TILE FETCH */
  236. if (unlikely(req->src_rect.h == 0 ||
  237. req->src_rect.w == 0)) {
  238. printk(KERN_ERR "mpd_ppp: src img of zero size!\n");
  239. return -EINVAL;
  240. }
  241. if (unlikely(req->dst_rect.h == 0 ||
  242. req->dst_rect.w == 0))
  243. return -EINVAL;
  244. /* do this first so that if this fails, the caller can always
  245. * safely call put_img */
  246. if (unlikely(get_img(&req->src, fb, &src_start, &src_len, &src_file))) {
  247. printk(KERN_ERR "mpd_ppp: could not retrieve src image from "
  248. "memory\n");
  249. return -EINVAL;
  250. }
  251. if (unlikely(get_img(&req->dst, fb, &dst_start, &dst_len, &dst_file))) {
  252. printk(KERN_ERR "mpd_ppp: could not retrieve dst image from "
  253. "memory\n");
  254. return -EINVAL;
  255. }
  256. mutex_lock(&mdp_mutex);
  257. /* transp_masking unimplemented */
  258. req->transp_mask = MDP_TRANSP_NOP;
  259. if (unlikely((req->transp_mask != MDP_TRANSP_NOP ||
  260. req->alpha != MDP_ALPHA_NOP ||
  261. HAS_ALPHA(req->src.format)) &&
  262. (req->flags & MDP_ROT_90 &&
  263. req->dst_rect.w <= 16 && req->dst_rect.h >= 16))) {
  264. int i;
  265. unsigned int tiles = req->dst_rect.h / 16;
  266. unsigned int remainder = req->dst_rect.h % 16;
  267. req->src_rect.w = 16*req->src_rect.w / req->dst_rect.h;
  268. req->dst_rect.h = 16;
  269. for (i = 0; i < tiles; i++) {
  270. enable_mdp_irq(mdp, DL0_ROI_DONE);
  271. ret = mdp_ppp_blit(mdp, req, src_file, src_start,
  272. src_len, dst_file, dst_start,
  273. dst_len);
  274. if (ret)
  275. goto err_bad_blit;
  276. ret = mdp_ppp_wait(mdp);
  277. if (ret)
  278. goto err_wait_failed;
  279. req->dst_rect.y += 16;
  280. req->src_rect.x += req->src_rect.w;
  281. }
  282. if (!remainder)
  283. goto end;
  284. req->src_rect.w = remainder*req->src_rect.w / req->dst_rect.h;
  285. req->dst_rect.h = remainder;
  286. }
  287. enable_mdp_irq(mdp, DL0_ROI_DONE);
  288. ret = mdp_ppp_blit(mdp, req, src_file, src_start, src_len, dst_file,
  289. dst_start,
  290. dst_len);
  291. if (ret)
  292. goto err_bad_blit;
  293. ret = mdp_ppp_wait(mdp);
  294. if (ret)
  295. goto err_wait_failed;
  296. end:
  297. put_img(src_file, dst_file);
  298. mutex_unlock(&mdp_mutex);
  299. return 0;
  300. err_bad_blit:
  301. disable_mdp_irq(mdp, DL0_ROI_DONE);
  302. err_wait_failed:
  303. put_img(src_file, dst_file);
  304. mutex_unlock(&mdp_mutex);
  305. return ret;
  306. }
  307. void mdp_set_grp_disp(struct mdp_device *mdp_dev, unsigned disp_id)
  308. {
  309. struct mdp_info *mdp = container_of(mdp_dev, struct mdp_info, mdp_dev);
  310. disp_id &= 0xf;
  311. mdp_writel(mdp, disp_id, MDP_FULL_BYPASS_WORD43);
  312. }
  313. int register_mdp_client(struct class_interface *cint)
  314. {
  315. if (!mdp_class) {
  316. pr_err("mdp: no mdp_class when registering mdp client\n");
  317. return -ENODEV;
  318. }
  319. cint->class = mdp_class;
  320. return class_interface_register(cint);
  321. }
  322. #include "mdp_csc_table.h"
  323. #include "mdp_scale_tables.h"
  324. int mdp_probe(struct platform_device *pdev)
  325. {
  326. struct resource *resource;
  327. int ret;
  328. int n;
  329. struct mdp_info *mdp;
  330. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  331. if (!resource) {
  332. pr_err("mdp: can not get mdp mem resource!\n");
  333. return -ENOMEM;
  334. }
  335. mdp = kzalloc(sizeof(struct mdp_info), GFP_KERNEL);
  336. if (!mdp)
  337. return -ENOMEM;
  338. mdp->irq = platform_get_irq(pdev, 0);
  339. if (mdp->irq < 0) {
  340. pr_err("mdp: can not get mdp irq\n");
  341. ret = mdp->irq;
  342. goto error_get_irq;
  343. }
  344. mdp->base = ioremap(resource->start,
  345. resource->end - resource->start);
  346. if (mdp->base == 0) {
  347. printk(KERN_ERR "msmfb: cannot allocate mdp regs!\n");
  348. ret = -ENOMEM;
  349. goto error_ioremap;
  350. }
  351. mdp->mdp_dev.dma = mdp_dma;
  352. mdp->mdp_dev.dma_wait = mdp_dma_wait;
  353. mdp->mdp_dev.blit = mdp_blit;
  354. mdp->mdp_dev.set_grp_disp = mdp_set_grp_disp;
  355. clk = clk_get(&pdev->dev, "mdp_clk");
  356. if (IS_ERR(clk)) {
  357. printk(KERN_INFO "mdp: failed to get mdp clk");
  358. return PTR_ERR(clk);
  359. }
  360. ret = request_irq(mdp->irq, mdp_isr, IRQF_DISABLED, "msm_mdp", mdp);
  361. if (ret)
  362. goto error_request_irq;
  363. disable_irq(mdp->irq);
  364. mdp_irq_mask = 0;
  365. /* debug interface write access */
  366. mdp_writel(mdp, 1, 0x60);
  367. mdp_writel(mdp, MDP_ANY_INTR_MASK, MDP_INTR_ENABLE);
  368. mdp_writel(mdp, 1, MDP_EBI2_PORTMAP_MODE);
  369. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x01f8);
  370. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x01fc);
  371. for (n = 0; n < ARRAY_SIZE(csc_table); n++)
  372. mdp_writel(mdp, csc_table[n].val, csc_table[n].reg);
  373. /* clear up unused fg/main registers */
  374. /* comp.plane 2&3 ystride */
  375. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x0120);
  376. /* unpacked pattern */
  377. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x012c);
  378. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x0130);
  379. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x0134);
  380. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x0158);
  381. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x015c);
  382. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x0160);
  383. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x0170);
  384. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x0174);
  385. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x017c);
  386. /* comp.plane 2 & 3 */
  387. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x0114);
  388. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x0118);
  389. /* clear unused bg registers */
  390. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x01c8);
  391. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x01d0);
  392. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x01dc);
  393. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x01e0);
  394. mdp_writel(mdp, 0, MDP_CMD_DEBUG_ACCESS_BASE + 0x01e4);
  395. for (n = 0; n < ARRAY_SIZE(mdp_upscale_table); n++)
  396. mdp_writel(mdp, mdp_upscale_table[n].val,
  397. mdp_upscale_table[n].reg);
  398. for (n = 0; n < 9; n++)
  399. mdp_writel(mdp, mdp_default_ccs[n], 0x40440 + 4 * n);
  400. mdp_writel(mdp, mdp_default_ccs[9], 0x40500 + 4 * 0);
  401. mdp_writel(mdp, mdp_default_ccs[10], 0x40500 + 4 * 0);
  402. mdp_writel(mdp, mdp_default_ccs[11], 0x40500 + 4 * 0);
  403. /* register mdp device */
  404. mdp->mdp_dev.dev.parent = &pdev->dev;
  405. mdp->mdp_dev.dev.class = mdp_class;
  406. dev_set_name(&mdp->mdp_dev.dev, "mdp%d", pdev->id);
  407. /* if you can remove the platform device you'd have to implement
  408. * this:
  409. mdp_dev.release = mdp_class; */
  410. ret = device_register(&mdp->mdp_dev.dev);
  411. if (ret)
  412. goto error_device_register;
  413. return 0;
  414. error_device_register:
  415. free_irq(mdp->irq, mdp);
  416. error_request_irq:
  417. iounmap(mdp->base);
  418. error_get_irq:
  419. error_ioremap:
  420. kfree(mdp);
  421. return ret;
  422. }
  423. static struct platform_driver msm_mdp_driver = {
  424. .probe = mdp_probe,
  425. .driver = {.name = "msm_mdp"},
  426. };
  427. static int __init mdp_init(void)
  428. {
  429. mdp_class = class_create(THIS_MODULE, "msm_mdp");
  430. if (IS_ERR(mdp_class)) {
  431. printk(KERN_ERR "Error creating mdp class\n");
  432. return PTR_ERR(mdp_class);
  433. }
  434. return platform_driver_register(&msm_mdp_driver);
  435. }
  436. subsys_initcall(mdp_init);