mdp_vsync.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /* Copyright (c) 2008-2009, 2012 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/time.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/hrtimer.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/clk.h>
  23. #include <mach/hardware.h>
  24. #include <linux/io.h>
  25. #include <asm/system.h>
  26. #include <asm/mach-types.h>
  27. #include <linux/semaphore.h>
  28. #include <linux/uaccess.h>
  29. #include <mach/gpio.h>
  30. #include "mdp.h"
  31. #include "msm_fb.h"
  32. #include "mddihost.h"
  33. #ifdef CONFIG_FB_MSM_MDP40
  34. #include "mdp4.h"
  35. #define MDP_SYNC_CFG_0 0x100
  36. #define MDP_SYNC_STATUS_0 0x10c
  37. #define MDP_SYNC_CFG_1 0x104
  38. #define MDP_SYNC_STATUS_1 0x110
  39. #define MDP_PRIM_VSYNC_OUT_CTRL 0x118
  40. #define MDP_SEC_VSYNC_OUT_CTRL 0x11C
  41. #define MDP_VSYNC_SEL 0x124
  42. #define MDP_PRIM_VSYNC_INIT_VAL 0x128
  43. #define MDP_SEC_VSYNC_INIT_VAL 0x12C
  44. #else
  45. #define MDP_SYNC_CFG_0 0x300
  46. #define MDP_SYNC_STATUS_0 0x30c
  47. #define MDP_PRIM_VSYNC_OUT_CTRL 0x318
  48. #define MDP_PRIM_VSYNC_INIT_VAL 0x328
  49. #endif
  50. extern mddi_lcd_type mddi_lcd_idx;
  51. extern spinlock_t mdp_spin_lock;
  52. extern struct workqueue_struct *mdp_vsync_wq;
  53. extern int lcdc_mode;
  54. extern int vsync_mode;
  55. #ifdef MDP_HW_VSYNC
  56. int vsync_above_th = 4;
  57. int vsync_start_th = 1;
  58. int vsync_load_cnt;
  59. int vsync_clk_status;
  60. DEFINE_MUTEX(vsync_clk_lock);
  61. static DEFINE_SPINLOCK(vsync_timer_lock);
  62. static struct clk *mdp_vsync_clk;
  63. static struct msm_fb_data_type *vsync_mfd;
  64. static unsigned char timer_shutdown_flag;
  65. static uint32 vsync_cnt_cfg;
  66. void vsync_clk_prepare_enable(void)
  67. {
  68. if (mdp_vsync_clk)
  69. clk_prepare_enable(mdp_vsync_clk);
  70. }
  71. void vsync_clk_disable_unprepare(void)
  72. {
  73. if (mdp_vsync_clk)
  74. clk_disable_unprepare(mdp_vsync_clk);
  75. }
  76. void mdp_hw_vsync_clk_enable(struct msm_fb_data_type *mfd)
  77. {
  78. if (vsync_clk_status == 1)
  79. return;
  80. mutex_lock(&vsync_clk_lock);
  81. if (mfd->use_mdp_vsync) {
  82. clk_prepare_enable(mdp_vsync_clk);
  83. vsync_clk_status = 1;
  84. }
  85. mutex_unlock(&vsync_clk_lock);
  86. }
  87. void mdp_hw_vsync_clk_disable(struct msm_fb_data_type *mfd)
  88. {
  89. if (vsync_clk_status == 0)
  90. return;
  91. mutex_lock(&vsync_clk_lock);
  92. if (mfd->use_mdp_vsync) {
  93. clk_disable_unprepare(mdp_vsync_clk);
  94. vsync_clk_status = 0;
  95. }
  96. mutex_unlock(&vsync_clk_lock);
  97. }
  98. static void mdp_set_vsync(unsigned long data);
  99. void mdp_vsync_clk_enable(void)
  100. {
  101. if (vsync_mfd) {
  102. mdp_hw_vsync_clk_enable(vsync_mfd);
  103. if (!vsync_mfd->vsync_resync_timer.function)
  104. mdp_set_vsync((unsigned long) vsync_mfd);
  105. }
  106. }
  107. void mdp_vsync_clk_disable(void)
  108. {
  109. if (vsync_mfd) {
  110. if (vsync_mfd->vsync_resync_timer.function) {
  111. spin_lock(&vsync_timer_lock);
  112. timer_shutdown_flag = 1;
  113. spin_unlock(&vsync_timer_lock);
  114. del_timer_sync(&vsync_mfd->vsync_resync_timer);
  115. spin_lock(&vsync_timer_lock);
  116. timer_shutdown_flag = 0;
  117. spin_unlock(&vsync_timer_lock);
  118. vsync_mfd->vsync_resync_timer.function = NULL;
  119. }
  120. mdp_hw_vsync_clk_disable(vsync_mfd);
  121. }
  122. }
  123. #endif
  124. static void mdp_set_vsync(unsigned long data)
  125. {
  126. struct msm_fb_data_type *mfd = (struct msm_fb_data_type *)data;
  127. struct msm_fb_panel_data *pdata = NULL;
  128. pdata = (struct msm_fb_panel_data *)mfd->pdev->dev.platform_data;
  129. vsync_mfd = mfd;
  130. init_timer(&mfd->vsync_resync_timer);
  131. if ((pdata) && (pdata->set_vsync_notifier == NULL))
  132. return;
  133. if ((mfd->panel_info.lcd.vsync_enable) && (mfd->panel_power_on)
  134. && (!mfd->vsync_handler_pending)) {
  135. mfd->vsync_handler_pending = TRUE;
  136. if (!queue_work(mdp_vsync_wq, &mfd->vsync_resync_worker)) {
  137. MSM_FB_INFO
  138. ("mdp_set_vsync: can't queue_work! -> needs to increase vsync_resync_timer_duration\n");
  139. }
  140. } else {
  141. MSM_FB_DEBUG
  142. ("mdp_set_vsync failed! EN:%d PWR:%d PENDING:%d\n",
  143. mfd->panel_info.lcd.vsync_enable, mfd->panel_power_on,
  144. mfd->vsync_handler_pending);
  145. }
  146. spin_lock(&vsync_timer_lock);
  147. if (!timer_shutdown_flag) {
  148. mfd->vsync_resync_timer.function = mdp_set_vsync;
  149. mfd->vsync_resync_timer.data = data;
  150. mfd->vsync_resync_timer.expires =
  151. jiffies + mfd->panel_info.lcd.vsync_notifier_period;
  152. add_timer(&mfd->vsync_resync_timer);
  153. }
  154. spin_unlock(&vsync_timer_lock);
  155. }
  156. static void mdp_vsync_handler(void *data)
  157. {
  158. struct msm_fb_data_type *mfd = (struct msm_fb_data_type *)data;
  159. if (vsync_clk_status == 0) {
  160. pr_debug("Warning: vsync clk is disabled\n");
  161. mfd->vsync_handler_pending = FALSE;
  162. return;
  163. }
  164. if (mfd->use_mdp_vsync) {
  165. #ifdef MDP_HW_VSYNC
  166. if (mfd->panel_power_on) {
  167. MDP_OUTP(MDP_BASE + MDP_SYNC_STATUS_0, vsync_load_cnt);
  168. #ifdef CONFIG_FB_MSM_MDP40
  169. if (mdp_hw_revision < MDP4_REVISION_V2_1)
  170. MDP_OUTP(MDP_BASE + MDP_SYNC_STATUS_1,
  171. vsync_load_cnt);
  172. #endif
  173. }
  174. #endif
  175. } else {
  176. mfd->last_vsync_timetick = ktime_get_real();
  177. }
  178. mfd->vsync_handler_pending = FALSE;
  179. }
  180. irqreturn_t mdp_hw_vsync_handler_proxy(int irq, void *data)
  181. {
  182. /*
  183. * ToDo: tried enabling/disabling GPIO MDP HW VSYNC interrupt
  184. * but getting inaccurate timing in mdp_vsync_handler()
  185. * disable_irq(MDP_HW_VSYNC_IRQ);
  186. */
  187. mdp_vsync_handler(data);
  188. return IRQ_HANDLED;
  189. }
  190. #ifdef MDP_HW_VSYNC
  191. static void mdp_set_sync_cfg_0(struct msm_fb_data_type *mfd, int vsync_cnt)
  192. {
  193. unsigned long cfg;
  194. cfg = mfd->total_lcd_lines - 1;
  195. cfg <<= MDP_SYNCFG_HGT_LOC;
  196. if (mfd->panel_info.lcd.hw_vsync_mode)
  197. cfg |= MDP_SYNCFG_VSYNC_EXT_EN;
  198. cfg |= (MDP_SYNCFG_VSYNC_INT_EN | vsync_cnt);
  199. MDP_OUTP(MDP_BASE + MDP_SYNC_CFG_0, cfg);
  200. }
  201. #ifdef CONFIG_FB_MSM_MDP40
  202. static void mdp_set_sync_cfg_1(struct msm_fb_data_type *mfd, int vsync_cnt)
  203. {
  204. unsigned long cfg;
  205. cfg = mfd->total_lcd_lines - 1;
  206. cfg <<= MDP_SYNCFG_HGT_LOC;
  207. if (mfd->panel_info.lcd.hw_vsync_mode)
  208. cfg |= MDP_SYNCFG_VSYNC_EXT_EN;
  209. cfg |= (MDP_SYNCFG_VSYNC_INT_EN | vsync_cnt);
  210. MDP_OUTP(MDP_BASE + MDP_SYNC_CFG_1, cfg);
  211. }
  212. #endif
  213. void mdp_vsync_cfg_regs(struct msm_fb_data_type *mfd,
  214. boolean first_time)
  215. {
  216. /* MDP cmd block enable */
  217. mdp_pipe_ctrl(MDP_CMD_BLOCK, MDP_BLOCK_POWER_ON,
  218. FALSE);
  219. if (first_time)
  220. mdp_hw_vsync_clk_enable(mfd);
  221. mdp_set_sync_cfg_0(mfd, vsync_cnt_cfg);
  222. #ifdef CONFIG_FB_MSM_MDP40
  223. if (mdp_hw_revision < MDP4_REVISION_V2_1)
  224. mdp_set_sync_cfg_1(mfd, vsync_cnt_cfg);
  225. #endif
  226. /*
  227. * load the last line + 1 to be in the
  228. * safety zone
  229. */
  230. vsync_load_cnt = mfd->panel_info.yres;
  231. /* line counter init value at the next pulse */
  232. MDP_OUTP(MDP_BASE + MDP_PRIM_VSYNC_INIT_VAL,
  233. vsync_load_cnt);
  234. #ifdef CONFIG_FB_MSM_MDP40
  235. if (mdp_hw_revision < MDP4_REVISION_V2_1) {
  236. MDP_OUTP(MDP_BASE + MDP_SEC_VSYNC_INIT_VAL,
  237. vsync_load_cnt);
  238. }
  239. #endif
  240. /*
  241. * external vsync source pulse width and
  242. * polarity flip
  243. */
  244. MDP_OUTP(MDP_BASE + MDP_PRIM_VSYNC_OUT_CTRL, BIT(0));
  245. #ifdef CONFIG_FB_MSM_MDP40
  246. if (mdp_hw_revision < MDP4_REVISION_V2_1) {
  247. MDP_OUTP(MDP_BASE + MDP_SEC_VSYNC_OUT_CTRL, BIT(0));
  248. MDP_OUTP(MDP_BASE + MDP_VSYNC_SEL, 0x20);
  249. }
  250. #endif
  251. /* threshold */
  252. MDP_OUTP(MDP_BASE + 0x200, (vsync_above_th << 16) |
  253. (vsync_start_th));
  254. if (first_time)
  255. mdp_hw_vsync_clk_disable(mfd);
  256. /* MDP cmd block disable */
  257. mdp_pipe_ctrl(MDP_CMD_BLOCK, MDP_BLOCK_POWER_OFF, FALSE);
  258. }
  259. #endif
  260. void mdp_config_vsync(struct platform_device *pdev,
  261. struct msm_fb_data_type *mfd)
  262. {
  263. /* vsync on primary lcd only for now */
  264. if ((mfd->dest != DISPLAY_LCD) || (mfd->panel_info.pdest != DISPLAY_1)
  265. || (!vsync_mode)) {
  266. goto err_handle;
  267. }
  268. vsync_clk_status = 0;
  269. if (mfd->panel_info.lcd.vsync_enable) {
  270. mfd->total_porch_lines = mfd->panel_info.lcd.v_back_porch +
  271. mfd->panel_info.lcd.v_front_porch +
  272. mfd->panel_info.lcd.v_pulse_width;
  273. mfd->total_lcd_lines =
  274. mfd->panel_info.yres + mfd->total_porch_lines;
  275. mfd->lcd_ref_usec_time =
  276. 100000000 / mfd->panel_info.lcd.refx100;
  277. mfd->vsync_handler_pending = FALSE;
  278. mfd->last_vsync_timetick.tv64 = 0;
  279. #ifdef MDP_HW_VSYNC
  280. if (mdp_vsync_clk == NULL)
  281. mdp_vsync_clk = clk_get(&pdev->dev, "vsync_clk");
  282. if (IS_ERR(mdp_vsync_clk)) {
  283. printk(KERN_ERR "error: can't get mdp_vsync_clk!\n");
  284. mfd->use_mdp_vsync = 0;
  285. } else
  286. mfd->use_mdp_vsync = 1;
  287. if (mfd->use_mdp_vsync) {
  288. uint32 vsync_cnt_cfg_dem;
  289. uint32 mdp_vsync_clk_speed_hz;
  290. mdp_vsync_clk_speed_hz = clk_get_rate(mdp_vsync_clk);
  291. if (mdp_vsync_clk_speed_hz == 0) {
  292. mfd->use_mdp_vsync = 0;
  293. } else {
  294. /*
  295. * Do this calculation in 2 steps for
  296. * rounding uint32 properly.
  297. */
  298. vsync_cnt_cfg_dem =
  299. (mfd->panel_info.lcd.refx100 *
  300. mfd->total_lcd_lines) / 100;
  301. vsync_cnt_cfg =
  302. (mdp_vsync_clk_speed_hz) /
  303. vsync_cnt_cfg_dem;
  304. mdp_vsync_cfg_regs(mfd, TRUE);
  305. }
  306. }
  307. #else
  308. mfd->use_mdp_vsync = 0;
  309. hrtimer_init(&mfd->dma_hrtimer, CLOCK_MONOTONIC,
  310. HRTIMER_MODE_REL);
  311. mfd->dma_hrtimer.function = mdp_dma2_vsync_hrtimer_handler;
  312. mfd->vsync_width_boundary = vmalloc(mfd->panel_info.xres * 4);
  313. #endif
  314. #ifdef CONFIG_FB_MSM_MDDI
  315. mfd->channel_irq = 0;
  316. if (mfd->panel_info.lcd.hw_vsync_mode) {
  317. u32 vsync_gpio = mfd->vsync_gpio;
  318. u32 ret;
  319. if (vsync_gpio == -1) {
  320. MSM_FB_INFO("vsync_gpio not defined!\n");
  321. goto err_handle;
  322. }
  323. ret = gpio_tlmm_config(GPIO_CFG
  324. (vsync_gpio,
  325. (mfd->use_mdp_vsync) ? 1 : 0,
  326. GPIO_CFG_INPUT,
  327. GPIO_CFG_PULL_DOWN,
  328. GPIO_CFG_2MA),
  329. GPIO_CFG_ENABLE);
  330. if (ret)
  331. goto err_handle;
  332. /*
  333. * if use_mdp_vsync, then no interrupt need since
  334. * mdp_vsync is feed directly to mdp to reset the
  335. * write pointer counter. therefore no irq_handler
  336. * need to reset write pointer counter.
  337. */
  338. if (!mfd->use_mdp_vsync) {
  339. mfd->channel_irq = MSM_GPIO_TO_INT(vsync_gpio);
  340. if (request_irq
  341. (mfd->channel_irq,
  342. &mdp_hw_vsync_handler_proxy,
  343. IRQF_TRIGGER_FALLING, "VSYNC_GPIO",
  344. (void *)mfd)) {
  345. MSM_FB_INFO
  346. ("irq=%d failed! vsync_gpio=%d\n",
  347. mfd->channel_irq,
  348. vsync_gpio);
  349. goto err_handle;
  350. }
  351. }
  352. }
  353. #endif
  354. mdp_hw_vsync_clk_enable(mfd);
  355. mdp_set_vsync((unsigned long)mfd);
  356. }
  357. return;
  358. err_handle:
  359. if (mfd->vsync_width_boundary)
  360. vfree(mfd->vsync_width_boundary);
  361. mfd->panel_info.lcd.vsync_enable = FALSE;
  362. printk(KERN_ERR "%s: failed!\n", __func__);
  363. }
  364. void mdp_vsync_resync_workqueue_handler(struct work_struct *work)
  365. {
  366. struct msm_fb_data_type *mfd = NULL;
  367. int vsync_fnc_enabled = FALSE;
  368. struct msm_fb_panel_data *pdata = NULL;
  369. mfd = container_of(work, struct msm_fb_data_type, vsync_resync_worker);
  370. if (mfd) {
  371. if (mfd->panel_power_on) {
  372. pdata =
  373. (struct msm_fb_panel_data *)mfd->pdev->dev.
  374. platform_data;
  375. if (pdata->set_vsync_notifier != NULL) {
  376. if (pdata->clk_func && !pdata->clk_func(2)) {
  377. mfd->vsync_handler_pending = FALSE;
  378. return;
  379. }
  380. pdata->set_vsync_notifier(
  381. mdp_vsync_handler,
  382. (void *)mfd);
  383. vsync_fnc_enabled = TRUE;
  384. }
  385. }
  386. }
  387. if ((mfd) && (!vsync_fnc_enabled))
  388. mfd->vsync_handler_pending = FALSE;
  389. }
  390. boolean mdp_hw_vsync_set_handler(msm_fb_vsync_handler_type handler, void *data)
  391. {
  392. /*
  393. * ToDo: tried enabling/disabling GPIO MDP HW VSYNC interrupt
  394. * but getting inaccurate timing in mdp_vsync_handler()
  395. * enable_irq(MDP_HW_VSYNC_IRQ);
  396. */
  397. return TRUE;
  398. }
  399. uint32 mdp_get_lcd_line_counter(struct msm_fb_data_type *mfd)
  400. {
  401. uint32 elapsed_usec_time;
  402. uint32 lcd_line;
  403. ktime_t last_vsync_timetick_local;
  404. ktime_t curr_time;
  405. unsigned long flag;
  406. if ((!mfd->panel_info.lcd.vsync_enable) || (!vsync_mode))
  407. return 0;
  408. spin_lock_irqsave(&mdp_spin_lock, flag);
  409. last_vsync_timetick_local = mfd->last_vsync_timetick;
  410. spin_unlock_irqrestore(&mdp_spin_lock, flag);
  411. curr_time = ktime_get_real();
  412. elapsed_usec_time = ktime_to_us(ktime_sub(curr_time,
  413. last_vsync_timetick_local));
  414. elapsed_usec_time = elapsed_usec_time % mfd->lcd_ref_usec_time;
  415. /* lcd line calculation referencing to line counter = 0 */
  416. lcd_line =
  417. (elapsed_usec_time * mfd->total_lcd_lines) / mfd->lcd_ref_usec_time;
  418. /* lcd line adjusment referencing to the actual line counter at vsync */
  419. lcd_line =
  420. (mfd->total_lcd_lines - mfd->panel_info.lcd.v_back_porch +
  421. lcd_line) % (mfd->total_lcd_lines + 1);
  422. if (lcd_line > mfd->total_lcd_lines) {
  423. MSM_FB_INFO
  424. ("mdp_get_lcd_line_counter: mdp_lcd_rd_cnt >= mfd->total_lcd_lines error!\n");
  425. }
  426. return lcd_line;
  427. }