venc.c 25 KB

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