venc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /*
  2. * linux/drivers/video/omap2/dss/venc.c
  3. *
  4. * Copyright (C) 2009 Nokia Corporation
  5. * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
  6. *
  7. * VENC settings from TI's DSS driver
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #define DSS_SUBSYS_NAME "VENC"
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/clk.h>
  25. #include <linux/err.h>
  26. #include <linux/io.h>
  27. #include <linux/mutex.h>
  28. #include <linux/completion.h>
  29. #include <linux/delay.h>
  30. #include <linux/string.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <linux/pm_runtime.h>
  35. #include <linux/of.h>
  36. #include <linux/component.h>
  37. #include <video/omapdss.h>
  38. #include "dss.h"
  39. #include "dss_features.h"
  40. /* Venc registers */
  41. #define VENC_REV_ID 0x00
  42. #define VENC_STATUS 0x04
  43. #define VENC_F_CONTROL 0x08
  44. #define VENC_VIDOUT_CTRL 0x10
  45. #define VENC_SYNC_CTRL 0x14
  46. #define VENC_LLEN 0x1C
  47. #define VENC_FLENS 0x20
  48. #define VENC_HFLTR_CTRL 0x24
  49. #define VENC_CC_CARR_WSS_CARR 0x28
  50. #define VENC_C_PHASE 0x2C
  51. #define VENC_GAIN_U 0x30
  52. #define VENC_GAIN_V 0x34
  53. #define VENC_GAIN_Y 0x38
  54. #define VENC_BLACK_LEVEL 0x3C
  55. #define VENC_BLANK_LEVEL 0x40
  56. #define VENC_X_COLOR 0x44
  57. #define VENC_M_CONTROL 0x48
  58. #define VENC_BSTAMP_WSS_DATA 0x4C
  59. #define VENC_S_CARR 0x50
  60. #define VENC_LINE21 0x54
  61. #define VENC_LN_SEL 0x58
  62. #define VENC_L21__WC_CTL 0x5C
  63. #define VENC_HTRIGGER_VTRIGGER 0x60
  64. #define VENC_SAVID__EAVID 0x64
  65. #define VENC_FLEN__FAL 0x68
  66. #define VENC_LAL__PHASE_RESET 0x6C
  67. #define VENC_HS_INT_START_STOP_X 0x70
  68. #define VENC_HS_EXT_START_STOP_X 0x74
  69. #define VENC_VS_INT_START_X 0x78
  70. #define VENC_VS_INT_STOP_X__VS_INT_START_Y 0x7C
  71. #define VENC_VS_INT_STOP_Y__VS_EXT_START_X 0x80
  72. #define VENC_VS_EXT_STOP_X__VS_EXT_START_Y 0x84
  73. #define VENC_VS_EXT_STOP_Y 0x88
  74. #define VENC_AVID_START_STOP_X 0x90
  75. #define VENC_AVID_START_STOP_Y 0x94
  76. #define VENC_FID_INT_START_X__FID_INT_START_Y 0xA0
  77. #define VENC_FID_INT_OFFSET_Y__FID_EXT_START_X 0xA4
  78. #define VENC_FID_EXT_START_Y__FID_EXT_OFFSET_Y 0xA8
  79. #define VENC_TVDETGP_INT_START_STOP_X 0xB0
  80. #define VENC_TVDETGP_INT_START_STOP_Y 0xB4
  81. #define VENC_GEN_CTRL 0xB8
  82. #define VENC_OUTPUT_CONTROL 0xC4
  83. #define VENC_OUTPUT_TEST 0xC8
  84. #define VENC_DAC_B__DAC_C 0xC8
  85. struct venc_config {
  86. u32 f_control;
  87. u32 vidout_ctrl;
  88. u32 sync_ctrl;
  89. u32 llen;
  90. u32 flens;
  91. u32 hfltr_ctrl;
  92. u32 cc_carr_wss_carr;
  93. u32 c_phase;
  94. u32 gain_u;
  95. u32 gain_v;
  96. u32 gain_y;
  97. u32 black_level;
  98. u32 blank_level;
  99. u32 x_color;
  100. u32 m_control;
  101. u32 bstamp_wss_data;
  102. u32 s_carr;
  103. u32 line21;
  104. u32 ln_sel;
  105. u32 l21__wc_ctl;
  106. u32 htrigger_vtrigger;
  107. u32 savid__eavid;
  108. u32 flen__fal;
  109. u32 lal__phase_reset;
  110. u32 hs_int_start_stop_x;
  111. u32 hs_ext_start_stop_x;
  112. u32 vs_int_start_x;
  113. u32 vs_int_stop_x__vs_int_start_y;
  114. u32 vs_int_stop_y__vs_ext_start_x;
  115. u32 vs_ext_stop_x__vs_ext_start_y;
  116. u32 vs_ext_stop_y;
  117. u32 avid_start_stop_x;
  118. u32 avid_start_stop_y;
  119. u32 fid_int_start_x__fid_int_start_y;
  120. u32 fid_int_offset_y__fid_ext_start_x;
  121. u32 fid_ext_start_y__fid_ext_offset_y;
  122. u32 tvdetgp_int_start_stop_x;
  123. u32 tvdetgp_int_start_stop_y;
  124. u32 gen_ctrl;
  125. };
  126. /* from TRM */
  127. static const struct venc_config venc_config_pal_trm = {
  128. .f_control = 0,
  129. .vidout_ctrl = 1,
  130. .sync_ctrl = 0x40,
  131. .llen = 0x35F, /* 863 */
  132. .flens = 0x270, /* 624 */
  133. .hfltr_ctrl = 0,
  134. .cc_carr_wss_carr = 0x2F7225ED,
  135. .c_phase = 0,
  136. .gain_u = 0x111,
  137. .gain_v = 0x181,
  138. .gain_y = 0x140,
  139. .black_level = 0x3B,
  140. .blank_level = 0x3B,
  141. .x_color = 0x7,
  142. .m_control = 0x2,
  143. .bstamp_wss_data = 0x3F,
  144. .s_carr = 0x2A098ACB,
  145. .line21 = 0,
  146. .ln_sel = 0x01290015,
  147. .l21__wc_ctl = 0x0000F603,
  148. .htrigger_vtrigger = 0,
  149. .savid__eavid = 0x06A70108,
  150. .flen__fal = 0x00180270,
  151. .lal__phase_reset = 0x00040135,
  152. .hs_int_start_stop_x = 0x00880358,
  153. .hs_ext_start_stop_x = 0x000F035F,
  154. .vs_int_start_x = 0x01A70000,
  155. .vs_int_stop_x__vs_int_start_y = 0x000001A7,
  156. .vs_int_stop_y__vs_ext_start_x = 0x01AF0000,
  157. .vs_ext_stop_x__vs_ext_start_y = 0x000101AF,
  158. .vs_ext_stop_y = 0x00000025,
  159. .avid_start_stop_x = 0x03530083,
  160. .avid_start_stop_y = 0x026C002E,
  161. .fid_int_start_x__fid_int_start_y = 0x0001008A,
  162. .fid_int_offset_y__fid_ext_start_x = 0x002E0138,
  163. .fid_ext_start_y__fid_ext_offset_y = 0x01380001,
  164. .tvdetgp_int_start_stop_x = 0x00140001,
  165. .tvdetgp_int_start_stop_y = 0x00010001,
  166. .gen_ctrl = 0x00FF0000,
  167. };
  168. /* from TRM */
  169. static const struct venc_config venc_config_ntsc_trm = {
  170. .f_control = 0,
  171. .vidout_ctrl = 1,
  172. .sync_ctrl = 0x8040,
  173. .llen = 0x359,
  174. .flens = 0x20C,
  175. .hfltr_ctrl = 0,
  176. .cc_carr_wss_carr = 0x043F2631,
  177. .c_phase = 0,
  178. .gain_u = 0x102,
  179. .gain_v = 0x16C,
  180. .gain_y = 0x12F,
  181. .black_level = 0x43,
  182. .blank_level = 0x38,
  183. .x_color = 0x7,
  184. .m_control = 0x1,
  185. .bstamp_wss_data = 0x38,
  186. .s_carr = 0x21F07C1F,
  187. .line21 = 0,
  188. .ln_sel = 0x01310011,
  189. .l21__wc_ctl = 0x0000F003,
  190. .htrigger_vtrigger = 0,
  191. .savid__eavid = 0x069300F4,
  192. .flen__fal = 0x0016020C,
  193. .lal__phase_reset = 0x00060107,
  194. .hs_int_start_stop_x = 0x008E0350,
  195. .hs_ext_start_stop_x = 0x000F0359,
  196. .vs_int_start_x = 0x01A00000,
  197. .vs_int_stop_x__vs_int_start_y = 0x020701A0,
  198. .vs_int_stop_y__vs_ext_start_x = 0x01AC0024,
  199. .vs_ext_stop_x__vs_ext_start_y = 0x020D01AC,
  200. .vs_ext_stop_y = 0x00000006,
  201. .avid_start_stop_x = 0x03480078,
  202. .avid_start_stop_y = 0x02060024,
  203. .fid_int_start_x__fid_int_start_y = 0x0001008A,
  204. .fid_int_offset_y__fid_ext_start_x = 0x01AC0106,
  205. .fid_ext_start_y__fid_ext_offset_y = 0x01060006,
  206. .tvdetgp_int_start_stop_x = 0x00140001,
  207. .tvdetgp_int_start_stop_y = 0x00010001,
  208. .gen_ctrl = 0x00F90000,
  209. };
  210. static const struct venc_config venc_config_pal_bdghi = {
  211. .f_control = 0,
  212. .vidout_ctrl = 0,
  213. .sync_ctrl = 0,
  214. .hfltr_ctrl = 0,
  215. .x_color = 0,
  216. .line21 = 0,
  217. .ln_sel = 21,
  218. .htrigger_vtrigger = 0,
  219. .tvdetgp_int_start_stop_x = 0x00140001,
  220. .tvdetgp_int_start_stop_y = 0x00010001,
  221. .gen_ctrl = 0x00FB0000,
  222. .llen = 864-1,
  223. .flens = 625-1,
  224. .cc_carr_wss_carr = 0x2F7625ED,
  225. .c_phase = 0xDF,
  226. .gain_u = 0x111,
  227. .gain_v = 0x181,
  228. .gain_y = 0x140,
  229. .black_level = 0x3e,
  230. .blank_level = 0x3e,
  231. .m_control = 0<<2 | 1<<1,
  232. .bstamp_wss_data = 0x42,
  233. .s_carr = 0x2a098acb,
  234. .l21__wc_ctl = 0<<13 | 0x16<<8 | 0<<0,
  235. .savid__eavid = 0x06A70108,
  236. .flen__fal = 23<<16 | 624<<0,
  237. .lal__phase_reset = 2<<17 | 310<<0,
  238. .hs_int_start_stop_x = 0x00920358,
  239. .hs_ext_start_stop_x = 0x000F035F,
  240. .vs_int_start_x = 0x1a7<<16,
  241. .vs_int_stop_x__vs_int_start_y = 0x000601A7,
  242. .vs_int_stop_y__vs_ext_start_x = 0x01AF0036,
  243. .vs_ext_stop_x__vs_ext_start_y = 0x27101af,
  244. .vs_ext_stop_y = 0x05,
  245. .avid_start_stop_x = 0x03530082,
  246. .avid_start_stop_y = 0x0270002E,
  247. .fid_int_start_x__fid_int_start_y = 0x0005008A,
  248. .fid_int_offset_y__fid_ext_start_x = 0x002E0138,
  249. .fid_ext_start_y__fid_ext_offset_y = 0x01380005,
  250. };
  251. const struct omap_video_timings omap_dss_pal_timings = {
  252. .x_res = 720,
  253. .y_res = 574,
  254. .pixelclock = 13500000,
  255. .hsw = 64,
  256. .hfp = 12,
  257. .hbp = 68,
  258. .vsw = 5,
  259. .vfp = 5,
  260. .vbp = 41,
  261. .interlace = true,
  262. };
  263. EXPORT_SYMBOL(omap_dss_pal_timings);
  264. const struct omap_video_timings omap_dss_ntsc_timings = {
  265. .x_res = 720,
  266. .y_res = 482,
  267. .pixelclock = 13500000,
  268. .hsw = 64,
  269. .hfp = 16,
  270. .hbp = 58,
  271. .vsw = 6,
  272. .vfp = 6,
  273. .vbp = 31,
  274. .interlace = true,
  275. };
  276. EXPORT_SYMBOL(omap_dss_ntsc_timings);
  277. static struct {
  278. struct platform_device *pdev;
  279. void __iomem *base;
  280. struct mutex venc_lock;
  281. u32 wss_data;
  282. struct regulator *vdda_dac_reg;
  283. struct clk *tv_dac_clk;
  284. struct omap_video_timings timings;
  285. enum omap_dss_venc_type type;
  286. bool invert_polarity;
  287. struct omap_dss_device output;
  288. } venc;
  289. static inline void venc_write_reg(int idx, u32 val)
  290. {
  291. __raw_writel(val, venc.base + idx);
  292. }
  293. static inline u32 venc_read_reg(int idx)
  294. {
  295. u32 l = __raw_readl(venc.base + idx);
  296. return l;
  297. }
  298. static void venc_write_config(const struct venc_config *config)
  299. {
  300. DSSDBG("write venc conf\n");
  301. venc_write_reg(VENC_LLEN, config->llen);
  302. venc_write_reg(VENC_FLENS, config->flens);
  303. venc_write_reg(VENC_CC_CARR_WSS_CARR, config->cc_carr_wss_carr);
  304. venc_write_reg(VENC_C_PHASE, config->c_phase);
  305. venc_write_reg(VENC_GAIN_U, config->gain_u);
  306. venc_write_reg(VENC_GAIN_V, config->gain_v);
  307. venc_write_reg(VENC_GAIN_Y, config->gain_y);
  308. venc_write_reg(VENC_BLACK_LEVEL, config->black_level);
  309. venc_write_reg(VENC_BLANK_LEVEL, config->blank_level);
  310. venc_write_reg(VENC_M_CONTROL, config->m_control);
  311. venc_write_reg(VENC_BSTAMP_WSS_DATA, config->bstamp_wss_data |
  312. venc.wss_data);
  313. venc_write_reg(VENC_S_CARR, config->s_carr);
  314. venc_write_reg(VENC_L21__WC_CTL, config->l21__wc_ctl);
  315. venc_write_reg(VENC_SAVID__EAVID, config->savid__eavid);
  316. venc_write_reg(VENC_FLEN__FAL, config->flen__fal);
  317. venc_write_reg(VENC_LAL__PHASE_RESET, config->lal__phase_reset);
  318. venc_write_reg(VENC_HS_INT_START_STOP_X, config->hs_int_start_stop_x);
  319. venc_write_reg(VENC_HS_EXT_START_STOP_X, config->hs_ext_start_stop_x);
  320. venc_write_reg(VENC_VS_INT_START_X, config->vs_int_start_x);
  321. venc_write_reg(VENC_VS_INT_STOP_X__VS_INT_START_Y,
  322. config->vs_int_stop_x__vs_int_start_y);
  323. venc_write_reg(VENC_VS_INT_STOP_Y__VS_EXT_START_X,
  324. config->vs_int_stop_y__vs_ext_start_x);
  325. venc_write_reg(VENC_VS_EXT_STOP_X__VS_EXT_START_Y,
  326. config->vs_ext_stop_x__vs_ext_start_y);
  327. venc_write_reg(VENC_VS_EXT_STOP_Y, config->vs_ext_stop_y);
  328. venc_write_reg(VENC_AVID_START_STOP_X, config->avid_start_stop_x);
  329. venc_write_reg(VENC_AVID_START_STOP_Y, config->avid_start_stop_y);
  330. venc_write_reg(VENC_FID_INT_START_X__FID_INT_START_Y,
  331. config->fid_int_start_x__fid_int_start_y);
  332. venc_write_reg(VENC_FID_INT_OFFSET_Y__FID_EXT_START_X,
  333. config->fid_int_offset_y__fid_ext_start_x);
  334. venc_write_reg(VENC_FID_EXT_START_Y__FID_EXT_OFFSET_Y,
  335. config->fid_ext_start_y__fid_ext_offset_y);
  336. venc_write_reg(VENC_DAC_B__DAC_C, venc_read_reg(VENC_DAC_B__DAC_C));
  337. venc_write_reg(VENC_VIDOUT_CTRL, config->vidout_ctrl);
  338. venc_write_reg(VENC_HFLTR_CTRL, config->hfltr_ctrl);
  339. venc_write_reg(VENC_X_COLOR, config->x_color);
  340. venc_write_reg(VENC_LINE21, config->line21);
  341. venc_write_reg(VENC_LN_SEL, config->ln_sel);
  342. venc_write_reg(VENC_HTRIGGER_VTRIGGER, config->htrigger_vtrigger);
  343. venc_write_reg(VENC_TVDETGP_INT_START_STOP_X,
  344. config->tvdetgp_int_start_stop_x);
  345. venc_write_reg(VENC_TVDETGP_INT_START_STOP_Y,
  346. config->tvdetgp_int_start_stop_y);
  347. venc_write_reg(VENC_GEN_CTRL, config->gen_ctrl);
  348. venc_write_reg(VENC_F_CONTROL, config->f_control);
  349. venc_write_reg(VENC_SYNC_CTRL, config->sync_ctrl);
  350. }
  351. static void venc_reset(void)
  352. {
  353. int t = 1000;
  354. venc_write_reg(VENC_F_CONTROL, 1<<8);
  355. while (venc_read_reg(VENC_F_CONTROL) & (1<<8)) {
  356. if (--t == 0) {
  357. DSSERR("Failed to reset venc\n");
  358. return;
  359. }
  360. }
  361. #ifdef CONFIG_OMAP2_DSS_SLEEP_AFTER_VENC_RESET
  362. /* the magical sleep that makes things work */
  363. /* XXX more info? What bug this circumvents? */
  364. msleep(20);
  365. #endif
  366. }
  367. static int venc_runtime_get(void)
  368. {
  369. int r;
  370. DSSDBG("venc_runtime_get\n");
  371. r = pm_runtime_get_sync(&venc.pdev->dev);
  372. WARN_ON(r < 0);
  373. return r < 0 ? r : 0;
  374. }
  375. static void venc_runtime_put(void)
  376. {
  377. int r;
  378. DSSDBG("venc_runtime_put\n");
  379. r = pm_runtime_put_sync(&venc.pdev->dev);
  380. WARN_ON(r < 0 && r != -ENOSYS);
  381. }
  382. static const struct venc_config *venc_timings_to_config(
  383. struct omap_video_timings *timings)
  384. {
  385. if (memcmp(&omap_dss_pal_timings, timings, sizeof(*timings)) == 0)
  386. return &venc_config_pal_trm;
  387. if (memcmp(&omap_dss_ntsc_timings, timings, sizeof(*timings)) == 0)
  388. return &venc_config_ntsc_trm;
  389. BUG();
  390. return NULL;
  391. }
  392. static int venc_power_on(struct omap_dss_device *dssdev)
  393. {
  394. struct omap_overlay_manager *mgr = venc.output.manager;
  395. u32 l;
  396. int r;
  397. r = venc_runtime_get();
  398. if (r)
  399. goto err0;
  400. venc_reset();
  401. venc_write_config(venc_timings_to_config(&venc.timings));
  402. dss_set_venc_output(venc.type);
  403. dss_set_dac_pwrdn_bgz(1);
  404. l = 0;
  405. if (venc.type == OMAP_DSS_VENC_TYPE_COMPOSITE)
  406. l |= 1 << 1;
  407. else /* S-Video */
  408. l |= (1 << 0) | (1 << 2);
  409. if (venc.invert_polarity == false)
  410. l |= 1 << 3;
  411. venc_write_reg(VENC_OUTPUT_CONTROL, l);
  412. dss_mgr_set_timings(mgr, &venc.timings);
  413. r = regulator_enable(venc.vdda_dac_reg);
  414. if (r)
  415. goto err1;
  416. r = dss_mgr_enable(mgr);
  417. if (r)
  418. goto err2;
  419. return 0;
  420. err2:
  421. regulator_disable(venc.vdda_dac_reg);
  422. err1:
  423. venc_write_reg(VENC_OUTPUT_CONTROL, 0);
  424. dss_set_dac_pwrdn_bgz(0);
  425. venc_runtime_put();
  426. err0:
  427. return r;
  428. }
  429. static void venc_power_off(struct omap_dss_device *dssdev)
  430. {
  431. struct omap_overlay_manager *mgr = venc.output.manager;
  432. venc_write_reg(VENC_OUTPUT_CONTROL, 0);
  433. dss_set_dac_pwrdn_bgz(0);
  434. dss_mgr_disable(mgr);
  435. regulator_disable(venc.vdda_dac_reg);
  436. venc_runtime_put();
  437. }
  438. static int venc_display_enable(struct omap_dss_device *dssdev)
  439. {
  440. struct omap_dss_device *out = &venc.output;
  441. int r;
  442. DSSDBG("venc_display_enable\n");
  443. mutex_lock(&venc.venc_lock);
  444. if (out == NULL || out->manager == NULL) {
  445. DSSERR("Failed to enable display: no output/manager\n");
  446. r = -ENODEV;
  447. goto err0;
  448. }
  449. r = venc_power_on(dssdev);
  450. if (r)
  451. goto err0;
  452. venc.wss_data = 0;
  453. mutex_unlock(&venc.venc_lock);
  454. return 0;
  455. err0:
  456. mutex_unlock(&venc.venc_lock);
  457. return r;
  458. }
  459. static void venc_display_disable(struct omap_dss_device *dssdev)
  460. {
  461. DSSDBG("venc_display_disable\n");
  462. mutex_lock(&venc.venc_lock);
  463. venc_power_off(dssdev);
  464. mutex_unlock(&venc.venc_lock);
  465. }
  466. static void venc_set_timings(struct omap_dss_device *dssdev,
  467. struct omap_video_timings *timings)
  468. {
  469. DSSDBG("venc_set_timings\n");
  470. mutex_lock(&venc.venc_lock);
  471. /* Reset WSS data when the TV standard changes. */
  472. if (memcmp(&venc.timings, timings, sizeof(*timings)))
  473. venc.wss_data = 0;
  474. venc.timings = *timings;
  475. dispc_set_tv_pclk(13500000);
  476. mutex_unlock(&venc.venc_lock);
  477. }
  478. static int venc_check_timings(struct omap_dss_device *dssdev,
  479. struct omap_video_timings *timings)
  480. {
  481. DSSDBG("venc_check_timings\n");
  482. if (memcmp(&omap_dss_pal_timings, timings, sizeof(*timings)) == 0)
  483. return 0;
  484. if (memcmp(&omap_dss_ntsc_timings, timings, sizeof(*timings)) == 0)
  485. return 0;
  486. return -EINVAL;
  487. }
  488. static void venc_get_timings(struct omap_dss_device *dssdev,
  489. struct omap_video_timings *timings)
  490. {
  491. mutex_lock(&venc.venc_lock);
  492. *timings = venc.timings;
  493. mutex_unlock(&venc.venc_lock);
  494. }
  495. static u32 venc_get_wss(struct omap_dss_device *dssdev)
  496. {
  497. /* Invert due to VENC_L21_WC_CTL:INV=1 */
  498. return (venc.wss_data >> 8) ^ 0xfffff;
  499. }
  500. static int venc_set_wss(struct omap_dss_device *dssdev, u32 wss)
  501. {
  502. const struct venc_config *config;
  503. int r;
  504. DSSDBG("venc_set_wss\n");
  505. mutex_lock(&venc.venc_lock);
  506. config = venc_timings_to_config(&venc.timings);
  507. /* Invert due to VENC_L21_WC_CTL:INV=1 */
  508. venc.wss_data = (wss ^ 0xfffff) << 8;
  509. r = venc_runtime_get();
  510. if (r)
  511. goto err;
  512. venc_write_reg(VENC_BSTAMP_WSS_DATA, config->bstamp_wss_data |
  513. venc.wss_data);
  514. venc_runtime_put();
  515. err:
  516. mutex_unlock(&venc.venc_lock);
  517. return r;
  518. }
  519. static void venc_set_type(struct omap_dss_device *dssdev,
  520. enum omap_dss_venc_type type)
  521. {
  522. mutex_lock(&venc.venc_lock);
  523. venc.type = type;
  524. mutex_unlock(&venc.venc_lock);
  525. }
  526. static void venc_invert_vid_out_polarity(struct omap_dss_device *dssdev,
  527. bool invert_polarity)
  528. {
  529. mutex_lock(&venc.venc_lock);
  530. venc.invert_polarity = invert_polarity;
  531. mutex_unlock(&venc.venc_lock);
  532. }
  533. static int venc_init_regulator(void)
  534. {
  535. struct regulator *vdda_dac;
  536. if (venc.vdda_dac_reg != NULL)
  537. return 0;
  538. if (venc.pdev->dev.of_node)
  539. vdda_dac = devm_regulator_get(&venc.pdev->dev, "vdda");
  540. else
  541. vdda_dac = devm_regulator_get(&venc.pdev->dev, "vdda_dac");
  542. if (IS_ERR(vdda_dac)) {
  543. if (PTR_ERR(vdda_dac) != -EPROBE_DEFER)
  544. DSSERR("can't get VDDA_DAC regulator\n");
  545. return PTR_ERR(vdda_dac);
  546. }
  547. venc.vdda_dac_reg = vdda_dac;
  548. return 0;
  549. }
  550. static void venc_dump_regs(struct seq_file *s)
  551. {
  552. #define DUMPREG(r) seq_printf(s, "%-35s %08x\n", #r, venc_read_reg(r))
  553. if (venc_runtime_get())
  554. return;
  555. DUMPREG(VENC_F_CONTROL);
  556. DUMPREG(VENC_VIDOUT_CTRL);
  557. DUMPREG(VENC_SYNC_CTRL);
  558. DUMPREG(VENC_LLEN);
  559. DUMPREG(VENC_FLENS);
  560. DUMPREG(VENC_HFLTR_CTRL);
  561. DUMPREG(VENC_CC_CARR_WSS_CARR);
  562. DUMPREG(VENC_C_PHASE);
  563. DUMPREG(VENC_GAIN_U);
  564. DUMPREG(VENC_GAIN_V);
  565. DUMPREG(VENC_GAIN_Y);
  566. DUMPREG(VENC_BLACK_LEVEL);
  567. DUMPREG(VENC_BLANK_LEVEL);
  568. DUMPREG(VENC_X_COLOR);
  569. DUMPREG(VENC_M_CONTROL);
  570. DUMPREG(VENC_BSTAMP_WSS_DATA);
  571. DUMPREG(VENC_S_CARR);
  572. DUMPREG(VENC_LINE21);
  573. DUMPREG(VENC_LN_SEL);
  574. DUMPREG(VENC_L21__WC_CTL);
  575. DUMPREG(VENC_HTRIGGER_VTRIGGER);
  576. DUMPREG(VENC_SAVID__EAVID);
  577. DUMPREG(VENC_FLEN__FAL);
  578. DUMPREG(VENC_LAL__PHASE_RESET);
  579. DUMPREG(VENC_HS_INT_START_STOP_X);
  580. DUMPREG(VENC_HS_EXT_START_STOP_X);
  581. DUMPREG(VENC_VS_INT_START_X);
  582. DUMPREG(VENC_VS_INT_STOP_X__VS_INT_START_Y);
  583. DUMPREG(VENC_VS_INT_STOP_Y__VS_EXT_START_X);
  584. DUMPREG(VENC_VS_EXT_STOP_X__VS_EXT_START_Y);
  585. DUMPREG(VENC_VS_EXT_STOP_Y);
  586. DUMPREG(VENC_AVID_START_STOP_X);
  587. DUMPREG(VENC_AVID_START_STOP_Y);
  588. DUMPREG(VENC_FID_INT_START_X__FID_INT_START_Y);
  589. DUMPREG(VENC_FID_INT_OFFSET_Y__FID_EXT_START_X);
  590. DUMPREG(VENC_FID_EXT_START_Y__FID_EXT_OFFSET_Y);
  591. DUMPREG(VENC_TVDETGP_INT_START_STOP_X);
  592. DUMPREG(VENC_TVDETGP_INT_START_STOP_Y);
  593. DUMPREG(VENC_GEN_CTRL);
  594. DUMPREG(VENC_OUTPUT_CONTROL);
  595. DUMPREG(VENC_OUTPUT_TEST);
  596. venc_runtime_put();
  597. #undef DUMPREG
  598. }
  599. static int venc_get_clocks(struct platform_device *pdev)
  600. {
  601. struct clk *clk;
  602. if (dss_has_feature(FEAT_VENC_REQUIRES_TV_DAC_CLK)) {
  603. clk = devm_clk_get(&pdev->dev, "tv_dac_clk");
  604. if (IS_ERR(clk)) {
  605. DSSERR("can't get tv_dac_clk\n");
  606. return PTR_ERR(clk);
  607. }
  608. } else {
  609. clk = NULL;
  610. }
  611. venc.tv_dac_clk = clk;
  612. return 0;
  613. }
  614. static int venc_connect(struct omap_dss_device *dssdev,
  615. struct omap_dss_device *dst)
  616. {
  617. struct omap_overlay_manager *mgr;
  618. int r;
  619. r = venc_init_regulator();
  620. if (r)
  621. return r;
  622. mgr = omap_dss_get_overlay_manager(dssdev->dispc_channel);
  623. if (!mgr)
  624. return -ENODEV;
  625. r = dss_mgr_connect(mgr, dssdev);
  626. if (r)
  627. return r;
  628. r = omapdss_output_set_device(dssdev, dst);
  629. if (r) {
  630. DSSERR("failed to connect output to new device: %s\n",
  631. dst->name);
  632. dss_mgr_disconnect(mgr, dssdev);
  633. return r;
  634. }
  635. return 0;
  636. }
  637. static void venc_disconnect(struct omap_dss_device *dssdev,
  638. struct omap_dss_device *dst)
  639. {
  640. WARN_ON(dst != dssdev->dst);
  641. if (dst != dssdev->dst)
  642. return;
  643. omapdss_output_unset_device(dssdev);
  644. if (dssdev->manager)
  645. dss_mgr_disconnect(dssdev->manager, dssdev);
  646. }
  647. static const struct omapdss_atv_ops venc_ops = {
  648. .connect = venc_connect,
  649. .disconnect = venc_disconnect,
  650. .enable = venc_display_enable,
  651. .disable = venc_display_disable,
  652. .check_timings = venc_check_timings,
  653. .set_timings = venc_set_timings,
  654. .get_timings = venc_get_timings,
  655. .set_type = venc_set_type,
  656. .invert_vid_out_polarity = venc_invert_vid_out_polarity,
  657. .set_wss = venc_set_wss,
  658. .get_wss = venc_get_wss,
  659. };
  660. static void venc_init_output(struct platform_device *pdev)
  661. {
  662. struct omap_dss_device *out = &venc.output;
  663. out->dev = &pdev->dev;
  664. out->id = OMAP_DSS_OUTPUT_VENC;
  665. out->output_type = OMAP_DISPLAY_TYPE_VENC;
  666. out->name = "venc.0";
  667. out->dispc_channel = OMAP_DSS_CHANNEL_DIGIT;
  668. out->ops.atv = &venc_ops;
  669. out->owner = THIS_MODULE;
  670. omapdss_register_output(out);
  671. }
  672. static void venc_uninit_output(struct platform_device *pdev)
  673. {
  674. struct omap_dss_device *out = &venc.output;
  675. omapdss_unregister_output(out);
  676. }
  677. static int venc_probe_of(struct platform_device *pdev)
  678. {
  679. struct device_node *node = pdev->dev.of_node;
  680. struct device_node *ep;
  681. u32 channels;
  682. int r;
  683. ep = omapdss_of_get_first_endpoint(node);
  684. if (!ep)
  685. return 0;
  686. venc.invert_polarity = of_property_read_bool(ep, "ti,invert-polarity");
  687. r = of_property_read_u32(ep, "ti,channels", &channels);
  688. if (r) {
  689. dev_err(&pdev->dev,
  690. "failed to read property 'ti,channels': %d\n", r);
  691. goto err;
  692. }
  693. switch (channels) {
  694. case 1:
  695. venc.type = OMAP_DSS_VENC_TYPE_COMPOSITE;
  696. break;
  697. case 2:
  698. venc.type = OMAP_DSS_VENC_TYPE_SVIDEO;
  699. break;
  700. default:
  701. dev_err(&pdev->dev, "bad channel propert '%d'\n", channels);
  702. r = -EINVAL;
  703. goto err;
  704. }
  705. of_node_put(ep);
  706. return 0;
  707. err:
  708. of_node_put(ep);
  709. return 0;
  710. }
  711. /* VENC HW IP initialisation */
  712. static int venc_bind(struct device *dev, struct device *master, void *data)
  713. {
  714. struct platform_device *pdev = to_platform_device(dev);
  715. u8 rev_id;
  716. struct resource *venc_mem;
  717. int r;
  718. venc.pdev = pdev;
  719. mutex_init(&venc.venc_lock);
  720. venc.wss_data = 0;
  721. venc_mem = platform_get_resource(venc.pdev, IORESOURCE_MEM, 0);
  722. if (!venc_mem) {
  723. DSSERR("can't get IORESOURCE_MEM VENC\n");
  724. return -EINVAL;
  725. }
  726. venc.base = devm_ioremap(&pdev->dev, venc_mem->start,
  727. resource_size(venc_mem));
  728. if (!venc.base) {
  729. DSSERR("can't ioremap VENC\n");
  730. return -ENOMEM;
  731. }
  732. r = venc_get_clocks(pdev);
  733. if (r)
  734. return r;
  735. pm_runtime_enable(&pdev->dev);
  736. r = venc_runtime_get();
  737. if (r)
  738. goto err_runtime_get;
  739. rev_id = (u8)(venc_read_reg(VENC_REV_ID) & 0xff);
  740. dev_dbg(&pdev->dev, "OMAP VENC rev %d\n", rev_id);
  741. venc_runtime_put();
  742. if (pdev->dev.of_node) {
  743. r = venc_probe_of(pdev);
  744. if (r) {
  745. DSSERR("Invalid DT data\n");
  746. goto err_probe_of;
  747. }
  748. }
  749. dss_debugfs_create_file("venc", venc_dump_regs);
  750. venc_init_output(pdev);
  751. return 0;
  752. err_probe_of:
  753. err_runtime_get:
  754. pm_runtime_disable(&pdev->dev);
  755. return r;
  756. }
  757. static void venc_unbind(struct device *dev, struct device *master, void *data)
  758. {
  759. struct platform_device *pdev = to_platform_device(dev);
  760. venc_uninit_output(pdev);
  761. pm_runtime_disable(&pdev->dev);
  762. }
  763. static const struct component_ops venc_component_ops = {
  764. .bind = venc_bind,
  765. .unbind = venc_unbind,
  766. };
  767. static int venc_probe(struct platform_device *pdev)
  768. {
  769. return component_add(&pdev->dev, &venc_component_ops);
  770. }
  771. static int venc_remove(struct platform_device *pdev)
  772. {
  773. component_del(&pdev->dev, &venc_component_ops);
  774. return 0;
  775. }
  776. static int venc_runtime_suspend(struct device *dev)
  777. {
  778. if (venc.tv_dac_clk)
  779. clk_disable_unprepare(venc.tv_dac_clk);
  780. dispc_runtime_put();
  781. return 0;
  782. }
  783. static int venc_runtime_resume(struct device *dev)
  784. {
  785. int r;
  786. r = dispc_runtime_get();
  787. if (r < 0)
  788. return r;
  789. if (venc.tv_dac_clk)
  790. clk_prepare_enable(venc.tv_dac_clk);
  791. return 0;
  792. }
  793. static const struct dev_pm_ops venc_pm_ops = {
  794. .runtime_suspend = venc_runtime_suspend,
  795. .runtime_resume = venc_runtime_resume,
  796. };
  797. static const struct of_device_id venc_of_match[] = {
  798. { .compatible = "ti,omap2-venc", },
  799. { .compatible = "ti,omap3-venc", },
  800. { .compatible = "ti,omap4-venc", },
  801. {},
  802. };
  803. static struct platform_driver omap_venchw_driver = {
  804. .probe = venc_probe,
  805. .remove = venc_remove,
  806. .driver = {
  807. .name = "omapdss_venc",
  808. .pm = &venc_pm_ops,
  809. .of_match_table = venc_of_match,
  810. .suppress_bind_attrs = true,
  811. },
  812. };
  813. int __init venc_init_platform_driver(void)
  814. {
  815. return platform_driver_register(&omap_venchw_driver);
  816. }
  817. void venc_uninit_platform_driver(void)
  818. {
  819. platform_driver_unregister(&omap_venchw_driver);
  820. }