vpif.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * vpif - Video Port Interface driver
  3. * VPIF is a receiver and transmitter for video data. It has two channels(0, 1)
  4. * that receiveing video byte stream and two channels(2, 3) for video output.
  5. * The hardware supports SDTV, HDTV formats, raw data capture.
  6. * Currently, the driver supports NTSC and PAL standards.
  7. *
  8. * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation version 2.
  13. *
  14. * This program is distributed .as is. WITHOUT ANY WARRANTY of any
  15. * kind, whether express or implied; without even the implied warranty
  16. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/io.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/v4l2-dv-timings.h>
  28. #include "vpif.h"
  29. MODULE_DESCRIPTION("TI DaVinci Video Port Interface driver");
  30. MODULE_LICENSE("GPL");
  31. #define VPIF_CH0_MAX_MODES 22
  32. #define VPIF_CH1_MAX_MODES 2
  33. #define VPIF_CH2_MAX_MODES 15
  34. #define VPIF_CH3_MAX_MODES 2
  35. spinlock_t vpif_lock;
  36. EXPORT_SYMBOL_GPL(vpif_lock);
  37. void __iomem *vpif_base;
  38. EXPORT_SYMBOL_GPL(vpif_base);
  39. /**
  40. * vpif_ch_params: video standard configuration parameters for vpif
  41. * The table must include all presets from supported subdevices.
  42. */
  43. const struct vpif_channel_config_params vpif_ch_params[] = {
  44. /* HDTV formats */
  45. {
  46. .name = "480p59_94",
  47. .width = 720,
  48. .height = 480,
  49. .frm_fmt = 1,
  50. .ycmux_mode = 0,
  51. .eav2sav = 138-8,
  52. .sav2eav = 720,
  53. .l1 = 1,
  54. .l3 = 43,
  55. .l5 = 523,
  56. .vsize = 525,
  57. .capture_format = 0,
  58. .vbi_supported = 0,
  59. .hd_sd = 1,
  60. .dv_timings = V4L2_DV_BT_CEA_720X480P59_94,
  61. },
  62. {
  63. .name = "576p50",
  64. .width = 720,
  65. .height = 576,
  66. .frm_fmt = 1,
  67. .ycmux_mode = 0,
  68. .eav2sav = 144-8,
  69. .sav2eav = 720,
  70. .l1 = 1,
  71. .l3 = 45,
  72. .l5 = 621,
  73. .vsize = 625,
  74. .capture_format = 0,
  75. .vbi_supported = 0,
  76. .hd_sd = 1,
  77. .dv_timings = V4L2_DV_BT_CEA_720X576P50,
  78. },
  79. {
  80. .name = "720p50",
  81. .width = 1280,
  82. .height = 720,
  83. .frm_fmt = 1,
  84. .ycmux_mode = 0,
  85. .eav2sav = 700-8,
  86. .sav2eav = 1280,
  87. .l1 = 1,
  88. .l3 = 26,
  89. .l5 = 746,
  90. .vsize = 750,
  91. .capture_format = 0,
  92. .vbi_supported = 0,
  93. .hd_sd = 1,
  94. .dv_timings = V4L2_DV_BT_CEA_1280X720P50,
  95. },
  96. {
  97. .name = "720p60",
  98. .width = 1280,
  99. .height = 720,
  100. .frm_fmt = 1,
  101. .ycmux_mode = 0,
  102. .eav2sav = 370 - 8,
  103. .sav2eav = 1280,
  104. .l1 = 1,
  105. .l3 = 26,
  106. .l5 = 746,
  107. .vsize = 750,
  108. .capture_format = 0,
  109. .vbi_supported = 0,
  110. .hd_sd = 1,
  111. .dv_timings = V4L2_DV_BT_CEA_1280X720P60,
  112. },
  113. {
  114. .name = "1080I50",
  115. .width = 1920,
  116. .height = 1080,
  117. .frm_fmt = 0,
  118. .ycmux_mode = 0,
  119. .eav2sav = 720 - 8,
  120. .sav2eav = 1920,
  121. .l1 = 1,
  122. .l3 = 21,
  123. .l5 = 561,
  124. .l7 = 563,
  125. .l9 = 584,
  126. .l11 = 1124,
  127. .vsize = 1125,
  128. .capture_format = 0,
  129. .vbi_supported = 0,
  130. .hd_sd = 1,
  131. .dv_timings = V4L2_DV_BT_CEA_1920X1080I50,
  132. },
  133. {
  134. .name = "1080I60",
  135. .width = 1920,
  136. .height = 1080,
  137. .frm_fmt = 0,
  138. .ycmux_mode = 0,
  139. .eav2sav = 280 - 8,
  140. .sav2eav = 1920,
  141. .l1 = 1,
  142. .l3 = 21,
  143. .l5 = 561,
  144. .l7 = 563,
  145. .l9 = 584,
  146. .l11 = 1124,
  147. .vsize = 1125,
  148. .capture_format = 0,
  149. .vbi_supported = 0,
  150. .hd_sd = 1,
  151. .dv_timings = V4L2_DV_BT_CEA_1920X1080I60,
  152. },
  153. {
  154. .name = "1080p60",
  155. .width = 1920,
  156. .height = 1080,
  157. .frm_fmt = 1,
  158. .ycmux_mode = 0,
  159. .eav2sav = 280 - 8,
  160. .sav2eav = 1920,
  161. .l1 = 1,
  162. .l3 = 42,
  163. .l5 = 1122,
  164. .vsize = 1125,
  165. .capture_format = 0,
  166. .vbi_supported = 0,
  167. .hd_sd = 1,
  168. .dv_timings = V4L2_DV_BT_CEA_1920X1080P60,
  169. },
  170. /* SDTV formats */
  171. {
  172. .name = "NTSC_M",
  173. .width = 720,
  174. .height = 480,
  175. .frm_fmt = 0,
  176. .ycmux_mode = 1,
  177. .eav2sav = 268,
  178. .sav2eav = 1440,
  179. .l1 = 1,
  180. .l3 = 23,
  181. .l5 = 263,
  182. .l7 = 266,
  183. .l9 = 286,
  184. .l11 = 525,
  185. .vsize = 525,
  186. .capture_format = 0,
  187. .vbi_supported = 1,
  188. .hd_sd = 0,
  189. .stdid = V4L2_STD_525_60,
  190. },
  191. {
  192. .name = "PAL_BDGHIK",
  193. .width = 720,
  194. .height = 576,
  195. .frm_fmt = 0,
  196. .ycmux_mode = 1,
  197. .eav2sav = 280,
  198. .sav2eav = 1440,
  199. .l1 = 1,
  200. .l3 = 23,
  201. .l5 = 311,
  202. .l7 = 313,
  203. .l9 = 336,
  204. .l11 = 624,
  205. .vsize = 625,
  206. .capture_format = 0,
  207. .vbi_supported = 1,
  208. .hd_sd = 0,
  209. .stdid = V4L2_STD_625_50,
  210. },
  211. };
  212. EXPORT_SYMBOL_GPL(vpif_ch_params);
  213. const unsigned int vpif_ch_params_count = ARRAY_SIZE(vpif_ch_params);
  214. EXPORT_SYMBOL_GPL(vpif_ch_params_count);
  215. static inline void vpif_wr_bit(u32 reg, u32 bit, u32 val)
  216. {
  217. if (val)
  218. vpif_set_bit(reg, bit);
  219. else
  220. vpif_clr_bit(reg, bit);
  221. }
  222. /* This structure is used to keep track of VPIF size register's offsets */
  223. struct vpif_registers {
  224. u32 h_cfg, v_cfg_00, v_cfg_01, v_cfg_02, v_cfg, ch_ctrl;
  225. u32 line_offset, vanc0_strt, vanc0_size, vanc1_strt;
  226. u32 vanc1_size, width_mask, len_mask;
  227. u8 max_modes;
  228. };
  229. static const struct vpif_registers vpifregs[VPIF_NUM_CHANNELS] = {
  230. /* Channel0 */
  231. {
  232. VPIF_CH0_H_CFG, VPIF_CH0_V_CFG_00, VPIF_CH0_V_CFG_01,
  233. VPIF_CH0_V_CFG_02, VPIF_CH0_V_CFG_03, VPIF_CH0_CTRL,
  234. VPIF_CH0_IMG_ADD_OFST, 0, 0, 0, 0, 0x1FFF, 0xFFF,
  235. VPIF_CH0_MAX_MODES,
  236. },
  237. /* Channel1 */
  238. {
  239. VPIF_CH1_H_CFG, VPIF_CH1_V_CFG_00, VPIF_CH1_V_CFG_01,
  240. VPIF_CH1_V_CFG_02, VPIF_CH1_V_CFG_03, VPIF_CH1_CTRL,
  241. VPIF_CH1_IMG_ADD_OFST, 0, 0, 0, 0, 0x1FFF, 0xFFF,
  242. VPIF_CH1_MAX_MODES,
  243. },
  244. /* Channel2 */
  245. {
  246. VPIF_CH2_H_CFG, VPIF_CH2_V_CFG_00, VPIF_CH2_V_CFG_01,
  247. VPIF_CH2_V_CFG_02, VPIF_CH2_V_CFG_03, VPIF_CH2_CTRL,
  248. VPIF_CH2_IMG_ADD_OFST, VPIF_CH2_VANC0_STRT, VPIF_CH2_VANC0_SIZE,
  249. VPIF_CH2_VANC1_STRT, VPIF_CH2_VANC1_SIZE, 0x7FF, 0x7FF,
  250. VPIF_CH2_MAX_MODES
  251. },
  252. /* Channel3 */
  253. {
  254. VPIF_CH3_H_CFG, VPIF_CH3_V_CFG_00, VPIF_CH3_V_CFG_01,
  255. VPIF_CH3_V_CFG_02, VPIF_CH3_V_CFG_03, VPIF_CH3_CTRL,
  256. VPIF_CH3_IMG_ADD_OFST, VPIF_CH3_VANC0_STRT, VPIF_CH3_VANC0_SIZE,
  257. VPIF_CH3_VANC1_STRT, VPIF_CH3_VANC1_SIZE, 0x7FF, 0x7FF,
  258. VPIF_CH3_MAX_MODES
  259. },
  260. };
  261. /* vpif_set_mode_info:
  262. * This function is used to set horizontal and vertical config parameters
  263. * As per the standard in the channel, configure the values of L1, L3,
  264. * L5, L7 L9, L11 in VPIF Register , also write width and height
  265. */
  266. static void vpif_set_mode_info(const struct vpif_channel_config_params *config,
  267. u8 channel_id, u8 config_channel_id)
  268. {
  269. u32 value;
  270. value = (config->eav2sav & vpifregs[config_channel_id].width_mask);
  271. value <<= VPIF_CH_LEN_SHIFT;
  272. value |= (config->sav2eav & vpifregs[config_channel_id].width_mask);
  273. regw(value, vpifregs[channel_id].h_cfg);
  274. value = (config->l1 & vpifregs[config_channel_id].len_mask);
  275. value <<= VPIF_CH_LEN_SHIFT;
  276. value |= (config->l3 & vpifregs[config_channel_id].len_mask);
  277. regw(value, vpifregs[channel_id].v_cfg_00);
  278. value = (config->l5 & vpifregs[config_channel_id].len_mask);
  279. value <<= VPIF_CH_LEN_SHIFT;
  280. value |= (config->l7 & vpifregs[config_channel_id].len_mask);
  281. regw(value, vpifregs[channel_id].v_cfg_01);
  282. value = (config->l9 & vpifregs[config_channel_id].len_mask);
  283. value <<= VPIF_CH_LEN_SHIFT;
  284. value |= (config->l11 & vpifregs[config_channel_id].len_mask);
  285. regw(value, vpifregs[channel_id].v_cfg_02);
  286. value = (config->vsize & vpifregs[config_channel_id].len_mask);
  287. regw(value, vpifregs[channel_id].v_cfg);
  288. }
  289. /* config_vpif_params
  290. * Function to set the parameters of a channel
  291. * Mainly modifies the channel ciontrol register
  292. * It sets frame format, yc mux mode
  293. */
  294. static void config_vpif_params(struct vpif_params *vpifparams,
  295. u8 channel_id, u8 found)
  296. {
  297. const struct vpif_channel_config_params *config = &vpifparams->std_info;
  298. u32 value, ch_nip, reg;
  299. u8 start, end;
  300. int i;
  301. start = channel_id;
  302. end = channel_id + found;
  303. for (i = start; i < end; i++) {
  304. reg = vpifregs[i].ch_ctrl;
  305. if (channel_id < 2)
  306. ch_nip = VPIF_CAPTURE_CH_NIP;
  307. else
  308. ch_nip = VPIF_DISPLAY_CH_NIP;
  309. vpif_wr_bit(reg, ch_nip, config->frm_fmt);
  310. vpif_wr_bit(reg, VPIF_CH_YC_MUX_BIT, config->ycmux_mode);
  311. vpif_wr_bit(reg, VPIF_CH_INPUT_FIELD_FRAME_BIT,
  312. vpifparams->video_params.storage_mode);
  313. /* Set raster scanning SDR Format */
  314. vpif_clr_bit(reg, VPIF_CH_SDR_FMT_BIT);
  315. vpif_wr_bit(reg, VPIF_CH_DATA_MODE_BIT, config->capture_format);
  316. if (channel_id > 1) /* Set the Pixel enable bit */
  317. vpif_set_bit(reg, VPIF_DISPLAY_PIX_EN_BIT);
  318. else if (config->capture_format) {
  319. /* Set the polarity of various pins */
  320. vpif_wr_bit(reg, VPIF_CH_FID_POLARITY_BIT,
  321. vpifparams->iface.fid_pol);
  322. vpif_wr_bit(reg, VPIF_CH_V_VALID_POLARITY_BIT,
  323. vpifparams->iface.vd_pol);
  324. vpif_wr_bit(reg, VPIF_CH_H_VALID_POLARITY_BIT,
  325. vpifparams->iface.hd_pol);
  326. value = regr(reg);
  327. /* Set data width */
  328. value &= ~(0x3u <<
  329. VPIF_CH_DATA_WIDTH_BIT);
  330. value |= ((vpifparams->params.data_sz) <<
  331. VPIF_CH_DATA_WIDTH_BIT);
  332. regw(value, reg);
  333. }
  334. /* Write the pitch in the driver */
  335. regw((vpifparams->video_params.hpitch),
  336. vpifregs[i].line_offset);
  337. }
  338. }
  339. /* vpif_set_video_params
  340. * This function is used to set video parameters in VPIF register
  341. */
  342. int vpif_set_video_params(struct vpif_params *vpifparams, u8 channel_id)
  343. {
  344. const struct vpif_channel_config_params *config = &vpifparams->std_info;
  345. int found = 1;
  346. vpif_set_mode_info(config, channel_id, channel_id);
  347. if (!config->ycmux_mode) {
  348. /* YC are on separate channels (HDTV formats) */
  349. vpif_set_mode_info(config, channel_id + 1, channel_id);
  350. found = 2;
  351. }
  352. config_vpif_params(vpifparams, channel_id, found);
  353. regw(0x80, VPIF_REQ_SIZE);
  354. regw(0x01, VPIF_EMULATION_CTRL);
  355. return found;
  356. }
  357. EXPORT_SYMBOL(vpif_set_video_params);
  358. void vpif_set_vbi_display_params(struct vpif_vbi_params *vbiparams,
  359. u8 channel_id)
  360. {
  361. u32 value;
  362. value = 0x3F8 & (vbiparams->hstart0);
  363. value |= 0x3FFFFFF & ((vbiparams->vstart0) << 16);
  364. regw(value, vpifregs[channel_id].vanc0_strt);
  365. value = 0x3F8 & (vbiparams->hstart1);
  366. value |= 0x3FFFFFF & ((vbiparams->vstart1) << 16);
  367. regw(value, vpifregs[channel_id].vanc1_strt);
  368. value = 0x3F8 & (vbiparams->hsize0);
  369. value |= 0x3FFFFFF & ((vbiparams->vsize0) << 16);
  370. regw(value, vpifregs[channel_id].vanc0_size);
  371. value = 0x3F8 & (vbiparams->hsize1);
  372. value |= 0x3FFFFFF & ((vbiparams->vsize1) << 16);
  373. regw(value, vpifregs[channel_id].vanc1_size);
  374. }
  375. EXPORT_SYMBOL(vpif_set_vbi_display_params);
  376. int vpif_channel_getfid(u8 channel_id)
  377. {
  378. return (regr(vpifregs[channel_id].ch_ctrl) & VPIF_CH_FID_MASK)
  379. >> VPIF_CH_FID_SHIFT;
  380. }
  381. EXPORT_SYMBOL(vpif_channel_getfid);
  382. static int vpif_probe(struct platform_device *pdev)
  383. {
  384. static struct resource *res;
  385. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  386. vpif_base = devm_ioremap_resource(&pdev->dev, res);
  387. if (IS_ERR(vpif_base))
  388. return PTR_ERR(vpif_base);
  389. pm_runtime_enable(&pdev->dev);
  390. pm_runtime_get(&pdev->dev);
  391. spin_lock_init(&vpif_lock);
  392. dev_info(&pdev->dev, "vpif probe success\n");
  393. return 0;
  394. }
  395. static int vpif_remove(struct platform_device *pdev)
  396. {
  397. pm_runtime_disable(&pdev->dev);
  398. return 0;
  399. }
  400. #ifdef CONFIG_PM
  401. static int vpif_suspend(struct device *dev)
  402. {
  403. pm_runtime_put(dev);
  404. return 0;
  405. }
  406. static int vpif_resume(struct device *dev)
  407. {
  408. pm_runtime_get(dev);
  409. return 0;
  410. }
  411. static const struct dev_pm_ops vpif_pm = {
  412. .suspend = vpif_suspend,
  413. .resume = vpif_resume,
  414. };
  415. #define vpif_pm_ops (&vpif_pm)
  416. #else
  417. #define vpif_pm_ops NULL
  418. #endif
  419. static struct platform_driver vpif_driver = {
  420. .driver = {
  421. .name = "vpif",
  422. .pm = vpif_pm_ops,
  423. },
  424. .remove = vpif_remove,
  425. .probe = vpif_probe,
  426. };
  427. static void vpif_exit(void)
  428. {
  429. platform_driver_unregister(&vpif_driver);
  430. }
  431. static int __init vpif_init(void)
  432. {
  433. return platform_driver_register(&vpif_driver);
  434. }
  435. subsys_initcall(vpif_init);
  436. module_exit(vpif_exit);