tvenc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* Copyright (c) 2008-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/delay.h>
  21. #include <mach/hardware.h>
  22. #include <linux/io.h>
  23. #include <linux/pm_runtime.h>
  24. #include <asm/system.h>
  25. #include <asm/mach-types.h>
  26. #include <linux/semaphore.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/clk.h>
  29. #include <linux/platform_device.h>
  30. #define TVENC_C
  31. #include "tvenc.h"
  32. #include "msm_fb.h"
  33. #include "mdp4.h"
  34. /* AXI rate in KHz */
  35. #define MSM_SYSTEM_BUS_RATE 128000000
  36. static int tvenc_probe(struct platform_device *pdev);
  37. static int tvenc_remove(struct platform_device *pdev);
  38. static int tvenc_off(struct platform_device *pdev);
  39. static int tvenc_on(struct platform_device *pdev);
  40. static struct platform_device *pdev_list[MSM_FB_MAX_DEV_LIST];
  41. static int pdev_list_cnt;
  42. static struct clk *tvenc_clk;
  43. static struct clk *tvdac_clk;
  44. static struct clk *tvenc_pclk;
  45. static struct clk *mdp_tv_clk;
  46. #ifdef CONFIG_FB_MSM_MDP40
  47. static struct clk *tv_src_clk;
  48. #endif
  49. #ifdef CONFIG_MSM_BUS_SCALING
  50. static uint32_t tvenc_bus_scale_handle;
  51. #endif
  52. static int tvenc_runtime_suspend(struct device *dev)
  53. {
  54. dev_dbg(dev, "pm_runtime: suspending...\n");
  55. return 0;
  56. }
  57. static int tvenc_runtime_resume(struct device *dev)
  58. {
  59. dev_dbg(dev, "pm_runtime: resuming...\n");
  60. return 0;
  61. }
  62. static struct dev_pm_ops tvenc_dev_pm_ops = {
  63. .runtime_suspend = tvenc_runtime_suspend,
  64. .runtime_resume = tvenc_runtime_resume,
  65. };
  66. static struct platform_driver tvenc_driver = {
  67. .probe = tvenc_probe,
  68. .remove = tvenc_remove,
  69. .suspend = NULL,
  70. .resume = NULL,
  71. .shutdown = NULL,
  72. .driver = {
  73. .name = "tvenc",
  74. .pm = &tvenc_dev_pm_ops
  75. },
  76. };
  77. int tvenc_set_encoder_clock(boolean clock_on)
  78. {
  79. int ret = 0;
  80. if (clock_on) {
  81. #ifdef CONFIG_FB_MSM_MDP40
  82. /* Consolidated clock used by both HDMI & TV encoder.
  83. Clock exists only in MDP4 and not in older versions */
  84. ret = clk_set_rate(tv_src_clk, 27000000);
  85. if (ret) {
  86. pr_err("%s: tvsrc_clk set rate failed! %d\n",
  87. __func__, ret);
  88. goto tvsrc_err;
  89. }
  90. #endif
  91. ret = clk_prepare_enable(tvenc_clk);
  92. if (ret) {
  93. pr_err("%s: tvenc_clk enable failed! %d\n",
  94. __func__, ret);
  95. goto tvsrc_err;
  96. }
  97. if (!IS_ERR(tvenc_pclk)) {
  98. ret = clk_prepare_enable(tvenc_pclk);
  99. if (ret) {
  100. pr_err("%s: tvenc_pclk enable failed! %d\n",
  101. __func__, ret);
  102. goto tvencp_err;
  103. }
  104. }
  105. return ret;
  106. } else {
  107. if (!IS_ERR(tvenc_pclk))
  108. clk_disable_unprepare(tvenc_pclk);
  109. clk_disable_unprepare(tvenc_clk);
  110. return ret;
  111. }
  112. tvencp_err:
  113. clk_disable_unprepare(tvenc_clk);
  114. tvsrc_err:
  115. return ret;
  116. }
  117. int tvenc_set_clock(boolean clock_on)
  118. {
  119. int ret = 0;
  120. if (clock_on) {
  121. if (tvenc_pdata->poll) {
  122. ret = tvenc_set_encoder_clock(CLOCK_ON);
  123. if (ret) {
  124. pr_err("%s: TVenc clock(s) enable failed! %d\n",
  125. __func__, ret);
  126. goto tvenc_err;
  127. }
  128. }
  129. ret = clk_prepare_enable(tvdac_clk);
  130. if (ret) {
  131. pr_err("%s: tvdac_clk enable failed! %d\n",
  132. __func__, ret);
  133. goto tvdac_err;
  134. }
  135. if (!IS_ERR(mdp_tv_clk)) {
  136. ret = clk_prepare_enable(mdp_tv_clk);
  137. if (ret) {
  138. pr_err("%s: mdp_tv_clk enable failed! %d\n",
  139. __func__, ret);
  140. goto mdptv_err;
  141. }
  142. }
  143. return ret;
  144. } else {
  145. if (!IS_ERR(mdp_tv_clk))
  146. clk_disable_unprepare(mdp_tv_clk);
  147. clk_disable_unprepare(tvdac_clk);
  148. if (tvenc_pdata->poll)
  149. tvenc_set_encoder_clock(CLOCK_OFF);
  150. return ret;
  151. }
  152. mdptv_err:
  153. clk_disable_unprepare(tvdac_clk);
  154. tvdac_err:
  155. tvenc_set_encoder_clock(CLOCK_OFF);
  156. tvenc_err:
  157. return ret;
  158. }
  159. static int tvenc_off(struct platform_device *pdev)
  160. {
  161. int ret = 0;
  162. struct msm_fb_data_type *mfd;
  163. mfd = platform_get_drvdata(pdev);
  164. ret = panel_next_off(pdev);
  165. if (ret)
  166. pr_err("%s: tvout_off failed! %d\n",
  167. __func__, ret);
  168. tvenc_set_clock(CLOCK_OFF);
  169. if (tvenc_pdata && tvenc_pdata->pm_vid_en)
  170. ret = tvenc_pdata->pm_vid_en(0);
  171. #ifdef CONFIG_MSM_BUS_SCALING
  172. if (tvenc_bus_scale_handle > 0)
  173. msm_bus_scale_client_update_request(tvenc_bus_scale_handle,
  174. 0);
  175. #else
  176. if (mfd->ebi1_clk)
  177. clk_disable_unprepare(mfd->ebi1_clk);
  178. #endif
  179. if (ret)
  180. pr_err("%s: pm_vid_en(off) failed! %d\n",
  181. __func__, ret);
  182. mdp4_extn_disp = 0;
  183. return ret;
  184. }
  185. static int tvenc_on(struct platform_device *pdev)
  186. {
  187. int ret = 0;
  188. #ifndef CONFIG_MSM_BUS_SCALING
  189. struct msm_fb_data_type *mfd = platform_get_drvdata(pdev);
  190. #endif
  191. #ifdef CONFIG_MSM_BUS_SCALING
  192. if (tvenc_bus_scale_handle > 0)
  193. msm_bus_scale_client_update_request(tvenc_bus_scale_handle,
  194. 1);
  195. #else
  196. if (mfd->ebi1_clk)
  197. clk_prepare_enable(mfd->ebi1_clk);
  198. #endif
  199. mdp4_extn_disp = 1;
  200. if (tvenc_pdata && tvenc_pdata->pm_vid_en)
  201. ret = tvenc_pdata->pm_vid_en(1);
  202. if (ret) {
  203. pr_err("%s: pm_vid_en(on) failed! %d\n",
  204. __func__, ret);
  205. return ret;
  206. }
  207. ret = tvenc_set_clock(CLOCK_ON);
  208. if (ret) {
  209. pr_err("%s: tvenc_set_clock(CLOCK_ON) failed! %d\n",
  210. __func__, ret);
  211. tvenc_pdata->pm_vid_en(0);
  212. goto error;
  213. }
  214. ret = panel_next_on(pdev);
  215. if (ret) {
  216. pr_err("%s: tvout_on failed! %d\n",
  217. __func__, ret);
  218. tvenc_set_clock(CLOCK_OFF);
  219. tvenc_pdata->pm_vid_en(0);
  220. }
  221. error:
  222. return ret;
  223. }
  224. void tvenc_gen_test_pattern(struct msm_fb_data_type *mfd)
  225. {
  226. uint32 reg = 0, i;
  227. reg = readl(MSM_TV_ENC_CTL);
  228. reg |= TVENC_CTL_TEST_PATT_EN;
  229. for (i = 0; i < 3; i++) {
  230. TV_OUT(TV_ENC_CTL, 0); /* disable TV encoder */
  231. switch (i) {
  232. /*
  233. * TV Encoder - Color Bar Test Pattern
  234. */
  235. case 0:
  236. reg |= TVENC_CTL_TPG_CLRBAR;
  237. break;
  238. /*
  239. * TV Encoder - Red Frame Test Pattern
  240. */
  241. case 1:
  242. reg |= TVENC_CTL_TPG_REDCLR;
  243. break;
  244. /*
  245. * TV Encoder - Modulated Ramp Test Pattern
  246. */
  247. default:
  248. reg |= TVENC_CTL_TPG_MODRAMP;
  249. break;
  250. }
  251. TV_OUT(TV_ENC_CTL, reg);
  252. mdelay(5000);
  253. switch (i) {
  254. /*
  255. * TV Encoder - Color Bar Test Pattern
  256. */
  257. case 0:
  258. reg &= ~TVENC_CTL_TPG_CLRBAR;
  259. break;
  260. /*
  261. * TV Encoder - Red Frame Test Pattern
  262. */
  263. case 1:
  264. reg &= ~TVENC_CTL_TPG_REDCLR;
  265. break;
  266. /*
  267. * TV Encoder - Modulated Ramp Test Pattern
  268. */
  269. default:
  270. reg &= ~TVENC_CTL_TPG_MODRAMP;
  271. break;
  272. }
  273. }
  274. }
  275. static int tvenc_resource_initialized;
  276. static int tvenc_probe(struct platform_device *pdev)
  277. {
  278. struct msm_fb_data_type *mfd;
  279. struct platform_device *mdp_dev = NULL;
  280. struct msm_fb_panel_data *pdata = NULL;
  281. int rc, ret;
  282. struct clk *ebi1_clk = NULL;
  283. if (pdev->id == 0) {
  284. tvenc_base = ioremap(pdev->resource[0].start,
  285. pdev->resource[0].end -
  286. pdev->resource[0].start + 1);
  287. if (!tvenc_base) {
  288. pr_err("tvenc_base ioremap failed!\n");
  289. return -ENOMEM;
  290. }
  291. tvenc_clk = clk_get(&pdev->dev, "enc_clk");
  292. tvdac_clk = clk_get(&pdev->dev, "dac_clk");
  293. tvenc_pclk = clk_get(&pdev->dev, "iface_clk");
  294. mdp_tv_clk = clk_get(&pdev->dev, "mdp_clk");
  295. #ifndef CONFIG_MSM_BUS_SCALING
  296. ebi1_clk = clk_get(&pdev->dev, "mem_clk");
  297. if (IS_ERR(ebi1_clk)) {
  298. rc = PTR_ERR(ebi1_clk);
  299. goto tvenc_probe_err;
  300. }
  301. clk_set_rate(ebi1_clk, MSM_SYSTEM_BUS_RATE);
  302. #endif
  303. #ifdef CONFIG_FB_MSM_MDP40
  304. tv_src_clk = clk_get(&pdev->dev, "src_clk");
  305. if (IS_ERR(tv_src_clk))
  306. tv_src_clk = tvenc_clk; /* Fallback to slave */
  307. #endif
  308. if (IS_ERR(tvenc_clk)) {
  309. pr_err("%s: error: can't get tvenc_clk!\n", __func__);
  310. return PTR_ERR(tvenc_clk);
  311. }
  312. if (IS_ERR(tvdac_clk)) {
  313. pr_err("%s: error: can't get tvdac_clk!\n", __func__);
  314. return PTR_ERR(tvdac_clk);
  315. }
  316. if (IS_ERR(tvenc_pclk)) {
  317. ret = PTR_ERR(tvenc_pclk);
  318. if (-ENOENT == ret)
  319. pr_info("%s: tvenc_pclk does not exist!\n",
  320. __func__);
  321. else {
  322. pr_err("%s: error: can't get tvenc_pclk!\n",
  323. __func__);
  324. return ret;
  325. }
  326. }
  327. if (IS_ERR(mdp_tv_clk)) {
  328. ret = PTR_ERR(mdp_tv_clk);
  329. if (-ENOENT == ret)
  330. pr_info("%s: mdp_tv_clk does not exist!\n",
  331. __func__);
  332. else {
  333. pr_err("%s: error: can't get mdp_tv_clk!\n",
  334. __func__);
  335. return ret;
  336. }
  337. }
  338. tvenc_pdata = pdev->dev.platform_data;
  339. tvenc_resource_initialized = 1;
  340. return 0;
  341. }
  342. if (!tvenc_resource_initialized)
  343. return -EPERM;
  344. mfd = platform_get_drvdata(pdev);
  345. mfd->ebi1_clk = ebi1_clk;
  346. if (!mfd)
  347. return -ENODEV;
  348. if (mfd->key != MFD_KEY)
  349. return -EINVAL;
  350. if (pdev_list_cnt >= MSM_FB_MAX_DEV_LIST)
  351. return -ENOMEM;
  352. if (tvenc_base == NULL)
  353. return -ENOMEM;
  354. mdp_dev = platform_device_alloc("mdp", pdev->id);
  355. if (!mdp_dev)
  356. return -ENOMEM;
  357. /*
  358. * link to the latest pdev
  359. */
  360. mfd->pdev = mdp_dev;
  361. mfd->dest = DISPLAY_TV;
  362. /*
  363. * alloc panel device data
  364. */
  365. if (platform_device_add_data
  366. (mdp_dev, pdev->dev.platform_data,
  367. sizeof(struct msm_fb_panel_data))) {
  368. pr_err("tvenc_probe: platform_device_add_data failed!\n");
  369. platform_device_put(mdp_dev);
  370. return -ENOMEM;
  371. }
  372. /*
  373. * data chain
  374. */
  375. pdata = mdp_dev->dev.platform_data;
  376. pdata->on = tvenc_on;
  377. pdata->off = tvenc_off;
  378. pdata->next = pdev;
  379. /*
  380. * get/set panel specific fb info
  381. */
  382. mfd->panel_info = pdata->panel_info;
  383. #ifdef CONFIG_FB_MSM_MDP40
  384. mfd->fb_imgType = MDP_RGB_565; /* base layer */
  385. #else
  386. mfd->fb_imgType = MDP_YCRYCB_H2V1;
  387. #endif
  388. #ifdef CONFIG_MSM_BUS_SCALING
  389. if (!tvenc_bus_scale_handle && tvenc_pdata &&
  390. tvenc_pdata->bus_scale_table) {
  391. tvenc_bus_scale_handle =
  392. msm_bus_scale_register_client(
  393. tvenc_pdata->bus_scale_table);
  394. if (!tvenc_bus_scale_handle) {
  395. printk(KERN_ERR "%s not able to get bus scale\n",
  396. __func__);
  397. }
  398. }
  399. #endif
  400. /*
  401. * set driver data
  402. */
  403. platform_set_drvdata(mdp_dev, mfd);
  404. /*
  405. * register in mdp driver
  406. */
  407. rc = platform_device_add(mdp_dev);
  408. if (rc)
  409. goto tvenc_probe_err;
  410. pm_runtime_set_active(&pdev->dev);
  411. pm_runtime_enable(&pdev->dev);
  412. pdev_list[pdev_list_cnt++] = pdev;
  413. return 0;
  414. tvenc_probe_err:
  415. #ifdef CONFIG_MSM_BUS_SCALING
  416. if (tvenc_pdata && tvenc_pdata->bus_scale_table &&
  417. tvenc_bus_scale_handle > 0) {
  418. msm_bus_scale_unregister_client(tvenc_bus_scale_handle);
  419. tvenc_bus_scale_handle = 0;
  420. }
  421. #endif
  422. platform_device_put(mdp_dev);
  423. return rc;
  424. }
  425. static int tvenc_remove(struct platform_device *pdev)
  426. {
  427. struct msm_fb_data_type *mfd;
  428. mfd = platform_get_drvdata(pdev);
  429. #ifdef CONFIG_MSM_BUS_SCALING
  430. if (tvenc_pdata && tvenc_pdata->bus_scale_table &&
  431. tvenc_bus_scale_handle > 0) {
  432. msm_bus_scale_unregister_client(tvenc_bus_scale_handle);
  433. tvenc_bus_scale_handle = 0;
  434. }
  435. #else
  436. clk_put(mfd->ebi1_clk);
  437. #endif
  438. pm_runtime_disable(&pdev->dev);
  439. return 0;
  440. }
  441. static int tvenc_register_driver(void)
  442. {
  443. return platform_driver_register(&tvenc_driver);
  444. }
  445. static int __init tvenc_driver_init(void)
  446. {
  447. return tvenc_register_driver();
  448. }
  449. module_init(tvenc_driver_init);